commands.js 32 KB

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