commands.js 45 KB

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