commands.js 44 KB

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