commands.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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) {
  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 = genericCreateCommand(command_type);
  233. el.data('fun').commands.push(new_cmd);
  234. renderCommand(new_cmd, $(function_container).find('.commands_list_div'), 3, function_obj);
  235. } else { // Entra nesse else, caso já existam outros comandos no bloco:
  236. findNearbyCommandToAddInFunctionScope(el, event, $(function_container).find('.commands_list_div'), function_obj, command_type);
  237. }
  238. } else {
  239. console.log("soltou em um comando");
  240. // descobrir em qual comando ele soltou:
  241. var hier_find = el.parentsUntil(".commands_list_div");
  242. var hierarquia_bottom_up = [];
  243. if (typeof el.data('command') !== 'undefined') {
  244. hierarquia_bottom_up.push(el.data('command'));
  245. }
  246. for (var i = 0; i < hier_find.length; i++) {
  247. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  248. hierarquia_bottom_up.push($(hier_find[i]).data('command'));
  249. }
  250. }
  251. console.log("comando em que soltou: ");
  252. console.log(hierarquia_bottom_up[0]);
  253. console.log("hierarquia de baixo para cima na árvore, de onde ele soltou: ");
  254. for (var i = 0; i < hierarquia_bottom_up.length; i++) {
  255. console.log(hierarquia_bottom_up[i]);
  256. }
  257. // Se for do tipo break, verificar se está no contexto correto:
  258. // Caso não esteja no contexto, apenas retorna sem dar continuidade:
  259. var is_correct_context = false;
  260. if (command_type == Models.COMMAND_TYPES.break) {
  261. for (var i = 0; i < hierarquia_bottom_up.length; i++) {
  262. if ((hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.repeatNtimes)
  263. || (hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.whiletrue)
  264. || (hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.dowhiletrue)
  265. || (hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.switch)) {
  266. is_correct_context = true;
  267. break;
  268. }
  269. }
  270. if (!is_correct_context) {
  271. console.error("Context not allowed to insert BREAK COMMAND!");
  272. return;
  273. }
  274. }
  275. // se a hierarquia possuir apenas um elemento, então está na raiz dos comandos:
  276. if (hierarquia_bottom_up.length == 1) {
  277. console.log('QQ1');
  278. var sub_elemento = false;
  279. for (var i = 0; i < hier_find.length; i++) {
  280. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  281. console.log('QQ2');
  282. findBeforeOrAfterCommandToAdd(hier_find[i], event, function_obj, command_type);
  283. sub_elemento = true;
  284. break;
  285. }
  286. }
  287. if (!sub_elemento) {
  288. console.log('QQ3');
  289. findBeforeOrAfterCommandToAdd(el[0], event, function_obj, command_type);
  290. }
  291. } else {
  292. console.log('QQ4');
  293. // caso exista mais de um elemento na hierarquia:
  294. if (typeof $(el).data('command') !== 'undefined') {
  295. console.log('QQ5');
  296. console.log("PPP1");
  297. insertCommandInBlockHierar(el[0], event, function_obj, command_type, hier_find, hierarquia_bottom_up);
  298. } else {
  299. console.log('QQ6');
  300. var sub_elemento = false;
  301. for (var i = 0; i < hier_find.length; i++) {
  302. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  303. console.log('QQ7');
  304. insertCommandInBlockHierar(hier_find[i], event, function_obj, command_type, hier_find, hierarquia_bottom_up);
  305. sub_elemento = true;
  306. break;
  307. }
  308. }
  309. }
  310. }
  311. }
  312. has_element_created_draged = false;
  313. which_element_is_draged = null;
  314. }
  315. function insertCommandInBlockHierar (el, event, function_obj, command_type, hier_dom, hier_obj) {
  316. var el_jq = $(el);
  317. var command_parent = el_jq.data('command');
  318. if ((el_jq.data('command').type == Models.COMMAND_TYPES.repeatNtimes) ||
  319. (el_jq.data('command').type == Models.COMMAND_TYPES.whiletrue) ||
  320. (el_jq.data('command').type == Models.COMMAND_TYPES.dowhiletrue) ||
  321. (el_jq.data('command').type == Models.COMMAND_TYPES.switch) ) {
  322. console.log('QQ17');
  323. if ((el_jq.data('command').type == Models.COMMAND_TYPES.repeatNtimes) ||
  324. (el_jq.data('command').type == Models.COMMAND_TYPES.whiletrue) ||
  325. (el_jq.data('command').type == Models.COMMAND_TYPES.dowhiletrue) ) {
  326. console.log('QQ18');
  327. // Se não tiver outro comando ainda no bloco, só adiciona:
  328. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  329. command_parent.commands_block = [];
  330. var recentComand = genericCreateCommand(command_type);
  331. command_parent.commands_block.push(recentComand);
  332. renderCommand(recentComand, el_jq.find('.block_commands'), 3, function_obj);
  333. } else { // Se já tem algum comando no bloco:
  334. findNearbyCommandToAddInBlockScope(el, event, el, function_obj, command_type, command_parent);
  335. }
  336. } else {
  337. // QUANDO FOR BLOCO DO TIPO IF OU SWITCH/CASE:
  338. addCommandToSwitchCase(event, function_obj, command_type);
  339. }
  340. } else {
  341. console.log('QQ19');
  342. // entra neste bloco, se soltou o comando sobre outro comando dentro de um subbloco:
  343. findBeforeOrAfterCommandToAddInsertBlock(el, event, function_obj, command_type);
  344. }
  345. }
  346. function findNearbyCommandToAddInBlockScope (el, event, node_list_commands, function_obj, command_type, command_parent) {
  347. var all_sub = $(node_list_commands).find('div.command_container');
  348. var menor_distancia = 999999999;
  349. var elemento_menor_distancia = null;
  350. var antes = true;
  351. var t_bot;
  352. var t_top;
  353. // Descobrindo o elemento mais próximo:
  354. for (var i = 0; i < all_sub.length; i++) {
  355. t_top = all_sub[i].getBoundingClientRect().top;
  356. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  357. if ((t_top - event.clientY) < menor_distancia) {
  358. menor_distancia = event.clientY - t_top;
  359. elemento_menor_distancia = all_sub[i];
  360. }
  361. }
  362. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  363. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  364. if ((borda_inferior - event.clientY) < menor_distancia) {
  365. var recentComand = genericCreateCommand(command_type);
  366. command_parent.commands_block.push(recentComand);
  367. //
  368. renderCommand(recentComand, node_list_commands, 3, function_obj);
  369. } else {
  370. var recentComand = genericCreateCommand(command_type);
  371. var index = command_parent.commands_block.indexOf($(elemento_menor_distancia).data('command'));
  372. if (index > -1) {
  373. command_parent.commands_block.splice(index, 0, recentComand);
  374. }
  375. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  376. }
  377. }
  378. function findBeforeOrAfterCommandToAddInsertBlock (el, event, function_obj, command_type) {
  379. var el_jq = $(el);
  380. var command_parent = $(el.parentNode.parentNode).data('command');
  381. var command_target = el_jq.data('command');
  382. var temp_parent = $(el.parentNode.parentNode);
  383. var is_in_else = false;
  384. if (!command_parent) {
  385. command_parent = el_jq.data('command');
  386. temp_parent = el_jq;
  387. var hier = el_jq.parentsUntil(".command_container");
  388. for (var i = 0; i < hier.length; i++) {
  389. var temp = $(hier[i]);
  390. if (typeof temp.data('else') != 'undefined') {
  391. is_in_else = true;
  392. }
  393. if (typeof temp.data('command') != 'undefined') {
  394. command_parent = temp.data('command');
  395. temp_parent = temp;
  396. }
  397. }
  398. }
  399. var hier = el_jq.parentsUntil(".command_container");
  400. for (var i = 0; i < hier.length; i++) {
  401. var temp = $(hier[i]);
  402. if (typeof temp.data('else') != 'undefined') {
  403. is_in_else = true;
  404. }
  405. }
  406. if (command_parent == command_target) {
  407. var hier = el_jq.parentsUntil(".command_container");
  408. for (var i = 0; i < hier.length; i++) {
  409. var temp = $(hier[i]);
  410. if (typeof temp.data('else') !== 'undefined') {
  411. is_in_else = true;
  412. break;
  413. }
  414. }
  415. }
  416. if ((command_parent.type != Models.COMMAND_TYPES.iftrue) && (command_parent.type != Models.COMMAND_TYPES.switch)) {
  417. var hier = temp_parent.parentsUntil(".all_cases_div");
  418. console.log("vou procurar!!");
  419. for (var i = 0; i < hier.length; i++) {
  420. console.log("estou vasculhando...");
  421. var temp = $(hier[i]);
  422. if (typeof temp.data('switchcase') !== 'undefined') {
  423. console.log("encontrei");
  424. command_parent = temp.data('switchcase');
  425. is_in_else = false;
  426. break;
  427. }
  428. }
  429. }
  430. console.log('debugging:');
  431. console.log('el_jq');
  432. console.log(el_jq);
  433. console.log('command_parent');
  434. console.log(command_parent);
  435. console.log('command_target');
  436. console.log(command_target);
  437. var menor_distancia = 999999999;
  438. var antes = true;
  439. var t_bot;
  440. var t_top;
  441. t_top = el.getBoundingClientRect().top;
  442. t_bot = el.getBoundingClientRect().top + el.getBoundingClientRect().height;
  443. var d_top = event.clientY - t_top; // distancia topo
  444. var d_bot = t_bot - event.clientY; // distancia baixo
  445. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  446. if (d_top < d_bot) {
  447. var recentComand = genericCreateCommand(command_type);
  448. console.log('MMM1');
  449. if (is_in_else) {
  450. console.log('MMM2');
  451. if (command_parent == command_target) {
  452. console.log('MMM3');
  453. if (command_parent.commands_else == null || command_parent.commands_else.length == 0) {
  454. command_parent.commands_else = [];
  455. var recentComand = genericCreateCommand(command_type);
  456. command_parent.commands_else.push(recentComand);
  457. renderCommand(recentComand, el_jq, 3, function_obj);
  458. } else { // Se já tem algum comando no bloco:
  459. findInBlockCorrectPlace(el_jq, event, function_obj, command_type, true);
  460. }
  461. return;
  462. }
  463. console.log('MMM7');
  464. var index = command_parent.commands_else.indexOf(command_target);
  465. if (index > -1) {
  466. command_parent.commands_else.splice(index, 0, recentComand);
  467. }
  468. renderCommand(recentComand, el, 1, function_obj);
  469. } else {
  470. console.log('MMM4');
  471. if (command_parent == command_target) {
  472. console.log('Nxxxx5');
  473. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  474. command_parent.commands_block = [];
  475. console.log('SSS4');
  476. var recentComand = genericCreateCommand(command_type);
  477. command_parent.commands_block.push(recentComand);
  478. renderCommand(recentComand, el_jq, 3, function_obj);
  479. } else {
  480. console.log('SSS5');
  481. findInBlockCorrectPlace(el_jq, event, function_obj, command_type);
  482. }
  483. return;
  484. }
  485. console.log('MMM6');
  486. var index = command_parent.commands_block.indexOf(command_target);
  487. if (index > -1) {
  488. command_parent.commands_block.splice(index, 0, recentComand);
  489. }
  490. renderCommand(recentComand, el, 1, function_obj);
  491. }
  492. } else {
  493. console.log('XXX1');
  494. var recentComand = genericCreateCommand(command_type);
  495. if (is_in_else) {
  496. if (command_parent == command_target) {
  497. console.log('MMM3');
  498. if (command_parent.commands_else == null || command_parent.commands_else.length == 0) {
  499. command_parent.commands_else = [];
  500. console.log('SSS1');
  501. var recentComand = genericCreateCommand(command_type);
  502. command_parent.commands_else.push(recentComand);
  503. renderCommand(recentComand, el_jq, 3, function_obj);
  504. } else { // Se já tem algum comando no bloco:
  505. console.log('SSS2');
  506. findInBlockCorrectPlace(el_jq, event, function_obj, command_type, true);
  507. }
  508. return;
  509. }
  510. console.log('XXX2');
  511. var index = command_parent.commands_else.indexOf(command_target);
  512. if (index > -1) {
  513. command_parent.commands_else.splice((index + 1), 0, recentComand);
  514. }
  515. renderCommand(recentComand, el, 2, function_obj);
  516. } else {
  517. if (command_parent == command_target) {
  518. console.log('Nxxxx78');
  519. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  520. command_parent.commands_block = [];
  521. var recentComand = genericCreateCommand(command_type);
  522. command_parent.commands_block.push(recentComand);
  523. console.log('SSS6');
  524. renderCommand(recentComand, el_jq, 3, function_obj);
  525. } else {
  526. console.log('SSS7');
  527. findInBlockCorrectPlace(el_jq, event, function_obj, command_type);
  528. }
  529. return;
  530. }
  531. console.log('XXX3');
  532. var index = command_parent.commands_block.indexOf(command_target);
  533. if (index > -1) {
  534. command_parent.commands_block.splice((index + 1), 0, recentComand);
  535. }
  536. renderCommand(recentComand, el, 2, function_obj);
  537. }
  538. }
  539. }
  540. function insertCommandInBlock (el, event, function_obj, command_type) {
  541. var el_jq = $(el);
  542. var command_parent = el_jq.data('command');
  543. if ((el_jq.data('command').type == Models.COMMAND_TYPES.repeatNtimes) ||
  544. (el_jq.data('command').type == Models.COMMAND_TYPES.whiletrue) ||
  545. (el_jq.data('command').type == Models.COMMAND_TYPES.dowhiletrue) ) {
  546. // Se não tiver outro comando ainda no bloco, só adiciona:
  547. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  548. command_parent.commands_block = [];
  549. var recentComand = genericCreateCommand(command_type);
  550. command_parent.commands_block.push(recentComand);
  551. renderCommand(recentComand, el_jq.find('.block_commands'), 3, function_obj);
  552. } else { // Se já tem algum comando no bloco:
  553. findInBlockCorrectPlace(el, event, function_obj, command_type);
  554. }
  555. } else if (el_jq.data('command').type == Models.COMMAND_TYPES.iftrue) {
  556. console.log('QQ9');
  557. // no if ou no else?
  558. var correct_div = $(document.elementFromPoint(event.pageX, event.pageY));
  559. var is_in_if = true;
  560. if (correct_div.data('if')) {
  561. is_in_if = true;
  562. } else if (correct_div.data('else')) {
  563. is_in_if = false;
  564. } else {
  565. var hier = correct_div.parentsUntil(".command_container");
  566. for (var i = 0; i < hier.length; i++) {
  567. var temp = $(hier[i]);
  568. if (typeof temp.data('if') !== 'undefined') {
  569. is_in_if = true;
  570. break;
  571. }
  572. if (typeof temp.data('else') !== 'undefined') {
  573. is_in_if = false;
  574. break;
  575. }
  576. }
  577. }
  578. if (is_in_if) {
  579. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  580. command_parent.commands_block = [];
  581. var recentComand = genericCreateCommand(command_type);
  582. command_parent.commands_block.push(recentComand);
  583. renderCommand(recentComand, el_jq.find('.commands_if'), 3, function_obj);
  584. } else { // Se já tem algum comando no bloco:
  585. findInBlockCorrectPlace(el_jq.find('.commands_if'), event, function_obj, command_type);
  586. }
  587. } else {
  588. if (command_parent.commands_else == null || command_parent.commands_else.length == 0) {
  589. command_parent.commands_else = [];
  590. var recentComand = genericCreateCommand(command_type);
  591. command_parent.commands_else.push(recentComand);
  592. renderCommand(recentComand, el_jq.find('.commands_else'), 3, function_obj);
  593. } else { // Se já tem algum comando no bloco:
  594. findInBlockCorrectPlace(el_jq.find('.commands_else'), event, function_obj, command_type, true);
  595. }
  596. }
  597. } else { // é do tipo switch
  598. console.log("está tentando inserir em um switch que está na raiz!");
  599. addCommandToSwitchCase(event, function_obj, command_type);
  600. }
  601. }
  602. function addCommandToSwitchCase (event, function_obj, command_type) {
  603. var el = $(document.elementFromPoint(event.clientX, event.clientY));
  604. var which_case = el.data('switchcase');
  605. var case_div = el;
  606. if (!which_case) {
  607. var hier_find = el.parentsUntil(".all_cases_div");
  608. for (var i = 0; i < hier_find.length; i++) {
  609. if (typeof $(hier_find[i]).data('switchcase') !== 'undefined') {
  610. which_case = $(hier_find[i]).data('switchcase');
  611. case_div = $(hier_find[i]);
  612. break;
  613. }
  614. }
  615. }
  616. if (which_case.commands_block == null || which_case.commands_block.length < 1) {
  617. which_case.commands_block = [];
  618. var recentComand = genericCreateCommand(command_type);
  619. which_case.commands_block.push(recentComand);
  620. renderCommand(recentComand, case_div.find('.case_commands_block'), 3, function_obj);
  621. } else {
  622. findInBlockCorrectPlaceInSwitchCase(which_case, case_div, event, function_obj, command_type);
  623. }
  624. }
  625. function findInBlockCorrectPlaceInSwitchCase (which_case, case_div, event, function_obj, command_type) {
  626. var all_sub = case_div.find('div.command_container');
  627. var menor_distancia = 999999999;
  628. var elemento_menor_distancia = null;
  629. var antes = true;
  630. var t_bot;
  631. var t_top;
  632. // Descobrindo o elemento mais próximo:
  633. for (var i = 0; i < all_sub.length; i++) {
  634. t_top = all_sub[i].getBoundingClientRect().top;
  635. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  636. if ((t_top - event.clientY) < menor_distancia) {
  637. menor_distancia = event.clientY - t_top;
  638. elemento_menor_distancia = all_sub[i];
  639. }
  640. }
  641. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  642. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  643. if ((borda_inferior - event.clientY) < menor_distancia) {
  644. var recentComand = genericCreateCommand(command_type);
  645. which_case.commands_block.push(recentComand);
  646. renderCommand(recentComand, $(case_div.find('.case_commands_block')[0]), 3, function_obj);
  647. } else {
  648. var recentComand = genericCreateCommand(command_type);
  649. var index = which_case.commands_block.indexOf($(elemento_menor_distancia).data('command'));
  650. if (index > -1) {
  651. which_case.commands_block.splice(index, 0, recentComand);
  652. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  653. }
  654. }
  655. }
  656. function findInBlockCorrectPlace (el, event, function_obj, command_type, is_in_else = false) {
  657. var el_jq = $(el);
  658. var all_sub = el_jq.find('div.command_container');
  659. var menor_distancia = 999999999;
  660. var elemento_menor_distancia = null;
  661. var antes = true;
  662. var t_bot;
  663. var t_top;
  664. // Descobrindo o elemento mais próximo:
  665. for (var i = 0; i < all_sub.length; i++) {
  666. t_top = all_sub[i].getBoundingClientRect().top;
  667. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  668. if ((t_top - event.clientY) < menor_distancia) {
  669. menor_distancia = event.clientY - t_top;
  670. elemento_menor_distancia = all_sub[i];
  671. }
  672. }
  673. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  674. console.log("menor_distancia: ");
  675. console.log(elemento_menor_distancia);
  676. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  677. if ((borda_inferior - event.clientY) < menor_distancia) {
  678. console.log('QQ11');
  679. var recentComand = genericCreateCommand(command_type);
  680. var command_parent = el_jq.data('command');
  681. if (is_in_else) {
  682. console.log('QQ15');
  683. command_parent.commands_else.push(recentComand);
  684. console.log('el_jq');
  685. console.log(el_jq);
  686. console.log("$(el_jq.find('.commands_else')[0]):: ");
  687. console.log($(el_jq.find('.commands_else')[0]));
  688. renderCommand(recentComand, el_jq, 3, function_obj);
  689. } else {
  690. console.log('QQ16');
  691. command_parent.commands_block.push(recentComand);
  692. renderCommand(recentComand, $(el_jq.find('.block_commands')[0]), 3, function_obj);
  693. }
  694. } else {
  695. console.log('QQ12');
  696. var recentComand = genericCreateCommand(command_type);
  697. var command_parent = el_jq.data('command');
  698. if (is_in_else) {
  699. var index = command_parent.commands_else.indexOf($(elemento_menor_distancia).data('command'));
  700. if (index > -1) {
  701. command_parent.commands_else.splice(index, 0, recentComand);
  702. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  703. }
  704. } else {
  705. var index = command_parent.commands_block.indexOf($(elemento_menor_distancia).data('command'));
  706. if (index > -1) {
  707. command_parent.commands_block.splice(index, 0, recentComand);
  708. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  709. }
  710. }
  711. }
  712. }
  713. function findBeforeOrAfterCommandToAdd (el, event, function_obj, command_type) {
  714. switch ($(el).data('command').type) {
  715. case Models.COMMAND_TYPES.iftrue:
  716. case Models.COMMAND_TYPES.switch:
  717. case Models.COMMAND_TYPES.repeatNtimes:
  718. case Models.COMMAND_TYPES.whiletrue:
  719. case Models.COMMAND_TYPES.dowhiletrue:
  720. insertCommandInBlock(el, event, function_obj, command_type);
  721. return;
  722. }
  723. var menor_distancia = 999999999;
  724. var antes = true;
  725. var t_bot;
  726. var t_top;
  727. t_top = el.getBoundingClientRect().top;
  728. t_bot = el.getBoundingClientRect().top + el.getBoundingClientRect().height;
  729. var d_top = event.clientY - t_top; // distancia topo
  730. var d_bot = t_bot - event.clientY; // distancia baixo
  731. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  732. if (d_top < d_bot) {
  733. var recentComand = genericCreateCommand(command_type);
  734. var index = function_obj.commands.indexOf($(el).data('command'));
  735. if (index > -1) {
  736. function_obj.commands.splice(index, 0, recentComand);
  737. }
  738. renderCommand(recentComand, el, 1, function_obj);
  739. } else {
  740. var recentComand = genericCreateCommand(command_type);
  741. var index = function_obj.commands.indexOf($(el).data('command'));
  742. if (index > -1) {
  743. function_obj.commands.splice((index + 1), 0, recentComand);
  744. }
  745. renderCommand(recentComand, el, 2, function_obj);
  746. }
  747. }
  748. function findNearbyCommandToAddInFunctionScope (el, event, node_list_commands, function_obj, command_type) {
  749. var all_sub = $(node_list_commands).find('div.command_container');
  750. var menor_distancia = 999999999;
  751. var elemento_menor_distancia = null;
  752. var antes = true;
  753. var t_bot;
  754. var t_top;
  755. // Descobrindo o elemento mais próximo:
  756. for (var i = 0; i < all_sub.length; i++) {
  757. t_top = all_sub[i].getBoundingClientRect().top;
  758. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  759. if ((t_top - event.clientY) < menor_distancia) {
  760. menor_distancia = event.clientY - t_top;
  761. elemento_menor_distancia = all_sub[i];
  762. }
  763. }
  764. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  765. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  766. if ((borda_inferior - event.clientY) < menor_distancia) {
  767. var recentComand = genericCreateCommand(command_type);
  768. function_obj.commands.push(recentComand);
  769. //
  770. renderCommand(recentComand, node_list_commands, 3, function_obj);
  771. } else {
  772. var recentComand = genericCreateCommand(command_type);
  773. var index = function_obj.commands.indexOf($(elemento_menor_distancia).data('command'));
  774. if (index > -1) {
  775. function_obj.commands.splice(index, 0, recentComand);
  776. }
  777. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  778. }
  779. }