commands.js 26 KB

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