commands_sidebar.js 43 KB

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