commands.js 42 KB

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