commands.js 41 KB

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