commands.js 42 KB

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