commands.js 42 KB

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