commands.js 42 KB

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