commands.js 41 KB

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