1
0

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. case Models.COMMAND_TYPES.repeatNtimes:
  381. return new Models.RepeatNTimes(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false),
  382. new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false),
  383. null, [new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)], null, null);
  384. case Models.COMMAND_TYPES.whiletrue:
  385. return new Models.WhileTrue([new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)], null);
  386. case Models.COMMAND_TYPES.dowhiletrue:
  387. return new Models.DoWhileTrue([new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)], null);
  388. case Models.COMMAND_TYPES.switch:
  389. var sc = [new Models.SwitchCase(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true))];
  390. return new Models.Switch(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.variable_and_function, null, null, null, true), sc);
  391. case Models.COMMAND_TYPES.return:
  392. return new Models.Return([new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  393. }
  394. }
  395. function dragTrash (event) {
  396. $('.ghost_div').remove();
  397. var trash = $('<i class="ui icon trash alternate outline"></i>');
  398. $('body').append(trash);
  399. trash.css('position', 'absolute');
  400. trash.css('top', event.clientY);
  401. trash.css('left', event.clientX - 20);
  402. trash.css('font-size', '3em');
  403. trash.css('display', 'none');
  404. function_container_active.find('.commands_list_div').off('mousemove');
  405. function_container_active.find('.commands_list_div').find("*").off('mousemove');
  406. trash.fadeIn( 200, function() {
  407. trash.fadeOut( 200, function() {
  408. trash.remove();
  409. } );
  410. });
  411. }
  412. function findSingleElement (searching, query) {
  413. if (!searching || !query) {
  414. return '';
  415. }
  416. if (searching.type == Models.COMMAND_TYPES.repeatNtimes ||
  417. searching.type == Models.COMMAND_TYPES.whiletrue ||
  418. searching.type == Models.COMMAND_TYPES.dowhiletrue) {
  419. if (searching.commands_block) {
  420. return '' + searching.commands_block.indexOf(query);
  421. } else {
  422. return '0';
  423. }
  424. } else if (searching.type == Models.COMMAND_TYPES.iftrue) {
  425. if (searching.commands_block == null) {
  426. searching.commands_block = [];
  427. }
  428. if (searching.commands_else == null) {
  429. searching.commands_else = [];
  430. }
  431. console.log('\n\nveja onde: ', searching.commands_block, ' \n\nquery: ', query);
  432. if (searching.commands_block.indexOf(query) >= 0) {
  433. return 'if[' + searching.commands_block.indexOf(query) + ']';
  434. } else {
  435. return 'else[' + searching.commands_else.indexOf(query) + ']';
  436. }
  437. } else if (searching.type == Models.COMMAND_TYPES.switch) {
  438. for (var i = 0; i < searching.cases.length; i++) {
  439. if (searching.cases[i].commands_block.indexOf(query) >= 0) {
  440. return 'case[' + searching.cases[i].commands_block.indexOf(query) + ']';
  441. }
  442. }
  443. }
  444. }
  445. function findPathRecursive (starter_index, hierarquia_bottom_up, function_obj, index_command = -1, actual = null) {
  446. var full_path = '';
  447. for (var i = 0; i <= hierarquia_bottom_up.length; i ++) {
  448. console.log('\n\n:: ', i, ':\n', hierarquia_bottom_up[i]);
  449. full_path += findSingleElement(hierarquia_bottom_up[i], hierarquia_bottom_up[i + 1]) + '-';
  450. }
  451. return full_path;
  452. }
  453. function manageCommand (function_obj, function_container, event, command_type) {
  454. $( ".created_element" ).each(function( index ) {
  455. $(this).remove();
  456. });
  457. var el = $(document.elementFromPoint(event.clientX, event.clientY));
  458. if (el.hasClass('ghost_div')) {
  459. if (el.closest('.command_container').length < 1) {
  460. console.log('\n\nvou tentar!!!!');
  461. console.log(el.closest('.commands_list_div'), '\n\n');
  462. el.closest('.commands_list_div').css('height', el.closest('.commands_list_div').css('height') + 30);
  463. //$('.ghost_div').remove();
  464. el = el.closest('.commands_list_div');
  465. }
  466. }
  467. console.log('soltou no: ');
  468. console.log(el);
  469. console.log(el.data('fun'));
  470. // Primeiro verificar se ele soltou no espaço da função correta:
  471. var hier = el.parentsUntil(".all_functions");
  472. var esta_correto = false;
  473. var esta_na_div_correta = false;
  474. if (el.hasClass("commands_list_div")) {
  475. esta_na_div_correta = true;
  476. }
  477. for (var i = 0; i < hier.length; i++) {
  478. var temp = $(hier[i]);
  479. if (temp.hasClass("commands_list_div")) {
  480. esta_na_div_correta = true;
  481. }
  482. if (temp.data('fun') == function_obj) {
  483. esta_correto = true;
  484. break;
  485. }
  486. }
  487. if (!esta_correto) {
  488. // has_element_created_draged = false;
  489. // which_element_is_draged = null;
  490. dragTrash(event);
  491. return;
  492. } else {
  493. if (!esta_na_div_correta) {
  494. // has_element_created_draged = false;
  495. // which_element_is_draged = null;
  496. dragTrash(event);
  497. return;
  498. }
  499. }
  500. var hierarquia_bottom_up = null;
  501. // Agora é descobrir qual o escopo para adicionar o comando:
  502. console.log('ev0');
  503. // Se o elemento clicado possuir o atributo "fun", então, é direto na div dos comandos:
  504. if (typeof el.data('fun') !== 'undefined') {
  505. // Se a lista de comandos estiver vazia, então é o primeiro.
  506. // Portanto, ele deve soltar o elemento obrigatoriamente no objeto vazio
  507. if ((el.data('fun').commands == null) || (el.data('fun').commands.length == 0)) {
  508. // pode adicionar
  509. el.data('fun').commands = [];
  510. var new_cmd = genericCreateCommand(command_type);
  511. registerUserEvent(function_obj.name, ActionTypes.INSERT_COMMAND, command_type, '/', 0);
  512. el.data('fun').commands.push(new_cmd);
  513. renderCommand(new_cmd, $(function_container).find('.commands_list_div'), 3, function_obj);
  514. } else { // Entra nesse else, caso já existam outros comandos no bloco:
  515. findNearbyCommandToAddInFunctionScope(el, event, $(function_container).find('.commands_list_div'), function_obj, command_type);
  516. }
  517. } else {
  518. console.log("soltou em um comando");
  519. // descobrir em qual comando ele soltou:
  520. var hier_find = el.parentsUntil(".commands_list_div");
  521. hierarquia_bottom_up = [];
  522. if (typeof el.data('command') !== 'undefined') {
  523. hierarquia_bottom_up.push(el.data('command'));
  524. }
  525. for (var i = 0; i < hier_find.length; i++) {
  526. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  527. hierarquia_bottom_up.push($(hier_find[i]).data('command'));
  528. }
  529. }
  530. console.log("comando em que soltou: ");
  531. console.log(hierarquia_bottom_up[0]);
  532. console.log("hierarquia de baixo para cima na árvore, de onde ele soltou: ");
  533. for (var i = 0; i < hierarquia_bottom_up.length; i++) {
  534. console.log(hierarquia_bottom_up[i]);
  535. }
  536. // Se for do tipo break, verificar se está no contexto correto:
  537. // Caso não esteja no contexto, apenas retorna sem dar continuidade:
  538. var is_correct_context = false;
  539. if (command_type == Models.COMMAND_TYPES.break) {
  540. for (var i = 0; i < hierarquia_bottom_up.length; i++) {
  541. if ((hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.repeatNtimes)
  542. || (hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.whiletrue)
  543. || (hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.dowhiletrue)
  544. || (hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.switch)) {
  545. is_correct_context = true;
  546. break;
  547. }
  548. }
  549. if (!is_correct_context) {
  550. console.error("Context not allowed to insert BREAK COMMAND!");
  551. return;
  552. }
  553. }
  554. // se a hierarquia possuir apenas um elemento, então está na raiz dos comandos:
  555. if (hierarquia_bottom_up.length == 1) {
  556. console.log('QQ1');
  557. var sub_elemento = false;
  558. for (var i = 0; i < hier_find.length; i++) {
  559. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  560. console.log('QQ2');
  561. findBeforeOrAfterCommandToAdd(hier_find[i], event, function_obj, command_type);
  562. sub_elemento = true;
  563. break;
  564. }
  565. }
  566. if (!sub_elemento) {
  567. console.log('QQ3');
  568. findBeforeOrAfterCommandToAdd(el[0], event, function_obj, command_type);
  569. }
  570. } else {
  571. console.log('QQ4');
  572. // caso exista mais de um elemento na hierarquia:
  573. if (typeof $(el).data('command') !== 'undefined') {
  574. console.log('QQ5');
  575. console.log("PPP1");
  576. insertCommandInBlockHierar(el[0], event, function_obj, command_type, hier_find, hierarquia_bottom_up);
  577. } else {
  578. console.log('QQ6');
  579. var sub_elemento = false;
  580. for (var i = 0; i < hier_find.length; i++) {
  581. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  582. console.log('QQ7');
  583. insertCommandInBlockHierar(hier_find[i], event, function_obj, command_type, hier_find, hierarquia_bottom_up);
  584. sub_elemento = true;
  585. break;
  586. }
  587. }
  588. }
  589. }
  590. }
  591. // has_element_created_draged = false;
  592. // which_element_is_draged = null;
  593. if (hierarquia_bottom_up) {
  594. console.log('\n\n:::índices::\n\n');
  595. var i = hierarquia_bottom_up.length - 1;
  596. var starter_index = window.program_obj.functions[window.program_obj.functions.indexOf(function_obj)].commands.indexOf(hierarquia_bottom_up[i]);
  597. var all_str_path = starter_index + '-' + findPathRecursive(starter_index, hierarquia_bottom_up, function_obj);
  598. registerUserEvent(function_obj.name, ActionTypes.INSERT_COMMAND, command_type, '/' , all_str_path);
  599. console.log('\n\nfull path: \n\n', all_str_path);
  600. console.log('fim dos índices');
  601. }
  602. renderAlgorithm();
  603. }
  604. function insertCommandInBlockHierar (el, event, function_obj, command_type, hier_dom, hier_obj) {
  605. var el_jq = $(el);
  606. var command_parent = el_jq.data('command');
  607. if ((el_jq.data('command').type == Models.COMMAND_TYPES.repeatNtimes) ||
  608. (el_jq.data('command').type == Models.COMMAND_TYPES.whiletrue) ||
  609. (el_jq.data('command').type == Models.COMMAND_TYPES.dowhiletrue) ||
  610. (el_jq.data('command').type == Models.COMMAND_TYPES.switch) ) {
  611. console.log('QQ17');
  612. if ((el_jq.data('command').type == Models.COMMAND_TYPES.repeatNtimes) ||
  613. (el_jq.data('command').type == Models.COMMAND_TYPES.whiletrue) ||
  614. (el_jq.data('command').type == Models.COMMAND_TYPES.dowhiletrue) ) {
  615. console.log('QQ18');
  616. // Se não tiver outro comando ainda no bloco, só adiciona:
  617. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  618. command_parent.commands_block = [];
  619. var recentComand = genericCreateCommand(command_type);
  620. command_parent.commands_block.push(recentComand);
  621. registerUserEvent(function_obj.name, ActionTypes.INSERT_COMMAND, command_type, '/' + el_jq.data('command').type, 0);
  622. renderCommand(recentComand, el_jq.find('.block_commands'), 3, function_obj);
  623. } else { // Se já tem algum comando no bloco:
  624. findNearbyCommandToAddInBlockScope(el, event, el, function_obj, command_type, command_parent);
  625. }
  626. } else {
  627. // QUANDO FOR BLOCO DO TIPO IF OU SWITCH/CASE:
  628. addCommandToSwitchCase(event, function_obj, command_type);
  629. }
  630. } else {
  631. console.log('QQ19');
  632. // entra neste bloco, se soltou o comando sobre outro comando dentro de um subbloco:
  633. findBeforeOrAfterCommandToAddInsertBlock(el, event, function_obj, command_type);
  634. }
  635. }
  636. function findNearbyCommandToAddInBlockScope (el, event, node_list_commands, function_obj, command_type, command_parent) {
  637. console.log("\n\n\n::COMANDOS:\n\n", el, event, node_list_commands, function_obj, command_type, command_parent);
  638. var all_sub = $(node_list_commands).find('div.command_container');
  639. var menor_distancia = 999999999;
  640. var elemento_menor_distancia = null;
  641. var antes = true;
  642. var t_bot;
  643. var t_top;
  644. // Descobrindo o elemento mais próximo:
  645. for (var i = 0; i < all_sub.length; i++) {
  646. t_top = all_sub[i].getBoundingClientRect().top;
  647. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  648. if ((t_top - event.clientY) < menor_distancia) {
  649. menor_distancia = event.clientY - t_top;
  650. elemento_menor_distancia = all_sub[i];
  651. }
  652. }
  653. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  654. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  655. if ((borda_inferior - event.clientY) < menor_distancia) {
  656. var recentComand = genericCreateCommand(command_type);
  657. command_parent.commands_block.push(recentComand);
  658. //
  659. renderCommand(recentComand, node_list_commands, 3, function_obj);
  660. } else {
  661. var recentComand = genericCreateCommand(command_type);
  662. var index = command_parent.commands_block.indexOf($(elemento_menor_distancia).data('command'));
  663. if (index > -1) {
  664. command_parent.commands_block.splice(index, 0, recentComand);
  665. }
  666. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  667. }
  668. }
  669. function findBeforeOrAfterCommandToAddInsertBlock (el, event, function_obj, command_type) {
  670. var el_jq = $(el);
  671. var command_parent = $(el.parentNode.parentNode).data('command');
  672. var command_target = el_jq.data('command');
  673. var temp_parent = $(el.parentNode.parentNode);
  674. var is_in_else = false;
  675. if (!command_parent) {
  676. command_parent = el_jq.data('command');
  677. temp_parent = el_jq;
  678. var hier = el_jq.parentsUntil(".command_container");
  679. for (var i = 0; i < hier.length; i++) {
  680. var temp = $(hier[i]);
  681. if (typeof temp.data('else') != 'undefined') {
  682. is_in_else = true;
  683. }
  684. if (typeof temp.data('command') != 'undefined') {
  685. command_parent = temp.data('command');
  686. temp_parent = temp;
  687. }
  688. }
  689. }
  690. var hier = el_jq.parentsUntil(".command_container");
  691. for (var i = 0; i < hier.length; i++) {
  692. var temp = $(hier[i]);
  693. if (typeof temp.data('else') != 'undefined') {
  694. is_in_else = true;
  695. }
  696. }
  697. if (command_parent == command_target) {
  698. var hier = el_jq.parentsUntil(".command_container");
  699. for (var i = 0; i < hier.length; i++) {
  700. var temp = $(hier[i]);
  701. if (typeof temp.data('else') !== 'undefined') {
  702. is_in_else = true;
  703. break;
  704. }
  705. }
  706. }
  707. if ((command_parent.type != Models.COMMAND_TYPES.iftrue) && (command_parent.type != Models.COMMAND_TYPES.switch)) {
  708. var hier = temp_parent.parentsUntil(".all_cases_div");
  709. console.log("vou procurar!!");
  710. for (var i = 0; i < hier.length; i++) {
  711. console.log("estou vasculhando...");
  712. var temp = $(hier[i]);
  713. if (typeof temp.data('switchcase') !== 'undefined') {
  714. console.log("encontrei");
  715. command_parent = temp.data('switchcase');
  716. is_in_else = false;
  717. break;
  718. }
  719. }
  720. }
  721. console.log('debugging:');
  722. console.log('el_jq');
  723. console.log(el_jq);
  724. console.log('command_parent');
  725. console.log(command_parent);
  726. console.log('command_target');
  727. console.log(command_target);
  728. var menor_distancia = 999999999;
  729. var antes = true;
  730. var t_bot;
  731. var t_top;
  732. t_top = el.getBoundingClientRect().top;
  733. t_bot = el.getBoundingClientRect().top + el.getBoundingClientRect().height;
  734. var d_top = event.clientY - t_top; // distancia topo
  735. var d_bot = t_bot - event.clientY; // distancia baixo
  736. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  737. if (d_top < d_bot) {
  738. var recentComand = genericCreateCommand(command_type);
  739. console.log('MMM1');
  740. if (is_in_else) {
  741. console.log('MMM2');
  742. if (command_parent == command_target) {
  743. console.log('MMM3');
  744. if (command_parent.commands_else == null || command_parent.commands_else.length == 0) {
  745. command_parent.commands_else = [];
  746. var recentComand = genericCreateCommand(command_type);
  747. command_parent.commands_else.push(recentComand);
  748. renderCommand(recentComand, el_jq, 3, function_obj);
  749. } else { // Se já tem algum comando no bloco:
  750. findInBlockCorrectPlace(el_jq, event, function_obj, command_type, true);
  751. }
  752. return;
  753. }
  754. console.log('MMM7');
  755. var index = command_parent.commands_else.indexOf(command_target);
  756. if (index > -1) {
  757. command_parent.commands_else.splice(index, 0, recentComand);
  758. }
  759. renderCommand(recentComand, el, 1, function_obj);
  760. } else {
  761. console.log('MMM4');
  762. if (command_parent == command_target) {
  763. console.log('Nxxxx5');
  764. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  765. command_parent.commands_block = [];
  766. console.log('SSS4');
  767. var recentComand = genericCreateCommand(command_type);
  768. command_parent.commands_block.push(recentComand);
  769. renderCommand(recentComand, el_jq, 3, function_obj);
  770. } else {
  771. console.log('SSS5');
  772. findInBlockCorrectPlace(el_jq, event, function_obj, command_type);
  773. }
  774. return;
  775. }
  776. console.log('MMM6');
  777. var index = command_parent.commands_block.indexOf(command_target);
  778. if (index > -1) {
  779. command_parent.commands_block.splice(index, 0, recentComand);
  780. }
  781. renderCommand(recentComand, el, 1, function_obj);
  782. }
  783. } else {
  784. console.log('XXX1');
  785. var recentComand = genericCreateCommand(command_type);
  786. if (is_in_else) {
  787. if (command_parent == command_target) {
  788. console.log('MMM3');
  789. if (command_parent.commands_else == null || command_parent.commands_else.length == 0) {
  790. command_parent.commands_else = [];
  791. console.log('SSS1');
  792. var recentComand = genericCreateCommand(command_type);
  793. command_parent.commands_else.push(recentComand);
  794. renderCommand(recentComand, el_jq, 3, function_obj);
  795. } else { // Se já tem algum comando no bloco:
  796. console.log('SSS2');
  797. findInBlockCorrectPlace(el_jq, event, function_obj, command_type, true);
  798. }
  799. return;
  800. }
  801. console.log('XXX2');
  802. var index = command_parent.commands_else.indexOf(command_target);
  803. if (index > -1) {
  804. command_parent.commands_else.splice((index + 1), 0, recentComand);
  805. }
  806. renderCommand(recentComand, el, 2, function_obj);
  807. } else {
  808. if (command_parent == command_target) {
  809. console.log('Nxxxx78');
  810. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  811. command_parent.commands_block = [];
  812. var recentComand = genericCreateCommand(command_type);
  813. command_parent.commands_block.push(recentComand);
  814. console.log('SSS6');
  815. renderCommand(recentComand, el_jq, 3, function_obj);
  816. } else {
  817. console.log('SSS7');
  818. findInBlockCorrectPlace(el_jq, event, function_obj, command_type);
  819. }
  820. return;
  821. }
  822. console.log('XXX3');
  823. var index = command_parent.commands_block.indexOf(command_target);
  824. if (index > -1) {
  825. command_parent.commands_block.splice((index + 1), 0, recentComand);
  826. }
  827. renderCommand(recentComand, el, 2, function_obj);
  828. }
  829. }
  830. }
  831. function insertCommandInBlock (el, event, function_obj, command_type) {
  832. var el_jq = $(el);
  833. var command_parent = el_jq.data('command');
  834. if ((el_jq.data('command').type == Models.COMMAND_TYPES.repeatNtimes) ||
  835. (el_jq.data('command').type == Models.COMMAND_TYPES.whiletrue) ||
  836. (el_jq.data('command').type == Models.COMMAND_TYPES.dowhiletrue) ) {
  837. // Se não tiver outro comando ainda no bloco, só adiciona:
  838. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  839. command_parent.commands_block = [];
  840. var recentComand = genericCreateCommand(command_type);
  841. command_parent.commands_block.push(recentComand);
  842. renderCommand(recentComand, el_jq.find('.block_commands'), 3, function_obj);
  843. } else { // Se já tem algum comando no bloco:
  844. findInBlockCorrectPlace(el, event, function_obj, command_type);
  845. }
  846. } else if (el_jq.data('command').type == Models.COMMAND_TYPES.iftrue) {
  847. console.log('QQ9');
  848. // no if ou no else?
  849. var correct_div = $(document.elementFromPoint(event.pageX, event.pageY));
  850. var is_in_if = true;
  851. if (correct_div.data('if')) {
  852. is_in_if = true;
  853. } else if (correct_div.data('else')) {
  854. is_in_if = false;
  855. } else {
  856. var hier = correct_div.parentsUntil(".command_container");
  857. for (var i = 0; i < hier.length; i++) {
  858. var temp = $(hier[i]);
  859. if (typeof temp.data('if') !== 'undefined') {
  860. is_in_if = true;
  861. break;
  862. }
  863. if (typeof temp.data('else') !== 'undefined') {
  864. is_in_if = false;
  865. break;
  866. }
  867. }
  868. }
  869. if (is_in_if) {
  870. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  871. command_parent.commands_block = [];
  872. var recentComand = genericCreateCommand(command_type);
  873. command_parent.commands_block.push(recentComand);
  874. renderCommand(recentComand, el_jq.find('.commands_if'), 3, function_obj);
  875. } else { // Se já tem algum comando no bloco:
  876. findInBlockCorrectPlace(el_jq.find('.commands_if'), event, function_obj, command_type);
  877. }
  878. } else {
  879. if (command_parent.commands_else == null || command_parent.commands_else.length == 0) {
  880. command_parent.commands_else = [];
  881. var recentComand = genericCreateCommand(command_type);
  882. command_parent.commands_else.push(recentComand);
  883. renderCommand(recentComand, el_jq.find('.commands_else'), 3, function_obj);
  884. } else { // Se já tem algum comando no bloco:
  885. findInBlockCorrectPlace(el_jq.find('.commands_else'), event, function_obj, command_type, true);
  886. }
  887. }
  888. } else { // é do tipo switch
  889. console.log("está tentando inserir em um switch que está na raiz!");
  890. addCommandToSwitchCase(event, function_obj, command_type);
  891. }
  892. }
  893. function addCommandToSwitchCase (event, function_obj, command_type) {
  894. var el = $(document.elementFromPoint(event.clientX, event.clientY));
  895. var which_case = el.data('switchcase');
  896. var case_div = el;
  897. if (!which_case) {
  898. var hier_find = el.parentsUntil(".all_cases_div");
  899. for (var i = 0; i < hier_find.length; i++) {
  900. if (typeof $(hier_find[i]).data('switchcase') !== 'undefined') {
  901. which_case = $(hier_find[i]).data('switchcase');
  902. case_div = $(hier_find[i]);
  903. break;
  904. }
  905. }
  906. }
  907. if (which_case.commands_block == null || which_case.commands_block.length < 1) {
  908. which_case.commands_block = [];
  909. var recentComand = genericCreateCommand(command_type);
  910. which_case.commands_block.push(recentComand);
  911. renderCommand(recentComand, case_div.find('.case_commands_block'), 3, function_obj);
  912. } else {
  913. findInBlockCorrectPlaceInSwitchCase(which_case, case_div, event, function_obj, command_type);
  914. }
  915. }
  916. function findInBlockCorrectPlaceInSwitchCase (which_case, case_div, event, function_obj, command_type) {
  917. var all_sub = case_div.find('div.command_container');
  918. var menor_distancia = 999999999;
  919. var elemento_menor_distancia = null;
  920. var antes = true;
  921. var t_bot;
  922. var t_top;
  923. // Descobrindo o elemento mais próximo:
  924. for (var i = 0; i < all_sub.length; i++) {
  925. t_top = all_sub[i].getBoundingClientRect().top;
  926. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  927. if ((t_top - event.clientY) < menor_distancia) {
  928. menor_distancia = event.clientY - t_top;
  929. elemento_menor_distancia = all_sub[i];
  930. }
  931. }
  932. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  933. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  934. if ((borda_inferior - event.clientY) < menor_distancia) {
  935. var recentComand = genericCreateCommand(command_type);
  936. which_case.commands_block.push(recentComand);
  937. renderCommand(recentComand, $(case_div.find('.case_commands_block')[0]), 3, function_obj);
  938. } else {
  939. var recentComand = genericCreateCommand(command_type);
  940. var index = which_case.commands_block.indexOf($(elemento_menor_distancia).data('command'));
  941. if (index > -1) {
  942. which_case.commands_block.splice(index, 0, recentComand);
  943. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  944. }
  945. }
  946. }
  947. function findInBlockCorrectPlace (el, event, function_obj, command_type, is_in_else = false) {
  948. var el_jq = $(el);
  949. var all_sub = el_jq.find('div.command_container');
  950. var menor_distancia = 999999999;
  951. var elemento_menor_distancia = null;
  952. var antes = true;
  953. var t_bot;
  954. var t_top;
  955. // Descobrindo o elemento mais próximo:
  956. for (var i = 0; i < all_sub.length; i++) {
  957. t_top = all_sub[i].getBoundingClientRect().top;
  958. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  959. if ((t_top - event.clientY) < menor_distancia) {
  960. menor_distancia = event.clientY - t_top;
  961. elemento_menor_distancia = all_sub[i];
  962. }
  963. }
  964. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  965. console.log("menor_distancia: ");
  966. console.log(elemento_menor_distancia);
  967. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  968. if ((borda_inferior - event.clientY) < menor_distancia) {
  969. console.log('QQ11');
  970. var recentComand = genericCreateCommand(command_type);
  971. var command_parent = el_jq.data('command');
  972. if (is_in_else) {
  973. console.log('QQ15');
  974. command_parent.commands_else.push(recentComand);
  975. console.log('el_jq');
  976. console.log(el_jq);
  977. console.log("$(el_jq.find('.commands_else')[0]):: ");
  978. console.log($(el_jq.find('.commands_else')[0]));
  979. renderCommand(recentComand, el_jq, 3, function_obj);
  980. } else {
  981. console.log('QQ16');
  982. command_parent.commands_block.push(recentComand);
  983. renderCommand(recentComand, $(el_jq.find('.block_commands')[0]), 3, function_obj);
  984. }
  985. } else {
  986. console.log('QQ12');
  987. var recentComand = genericCreateCommand(command_type);
  988. var command_parent = el_jq.data('command');
  989. if (is_in_else) {
  990. var index = command_parent.commands_else.indexOf($(elemento_menor_distancia).data('command'));
  991. if (index > -1) {
  992. command_parent.commands_else.splice(index, 0, recentComand);
  993. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  994. }
  995. } else {
  996. var index = command_parent.commands_block.indexOf($(elemento_menor_distancia).data('command'));
  997. if (index > -1) {
  998. command_parent.commands_block.splice(index, 0, recentComand);
  999. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  1000. }
  1001. }
  1002. }
  1003. }
  1004. function findBeforeOrAfterCommandToAdd (el, event, function_obj, command_type) {
  1005. switch ($(el).data('command').type) {
  1006. case Models.COMMAND_TYPES.iftrue:
  1007. case Models.COMMAND_TYPES.switch:
  1008. case Models.COMMAND_TYPES.repeatNtimes:
  1009. case Models.COMMAND_TYPES.whiletrue:
  1010. case Models.COMMAND_TYPES.dowhiletrue:
  1011. insertCommandInBlock(el, event, function_obj, command_type);
  1012. return;
  1013. }
  1014. var menor_distancia = 999999999;
  1015. var antes = true;
  1016. var t_bot;
  1017. var t_top;
  1018. t_top = el.getBoundingClientRect().top;
  1019. t_bot = el.getBoundingClientRect().top + el.getBoundingClientRect().height;
  1020. var d_top = event.clientY - t_top; // distancia topo
  1021. var d_bot = t_bot - event.clientY; // distancia baixo
  1022. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  1023. if (d_top < d_bot) {
  1024. var recentComand = genericCreateCommand(command_type);
  1025. var index = function_obj.commands.indexOf($(el).data('command'));
  1026. if (index > -1) {
  1027. function_obj.commands.splice(index, 0, recentComand);
  1028. }
  1029. renderCommand(recentComand, el, 1, function_obj);
  1030. registerUserEvent(function_obj.name, ActionTypes.INSERT_COMMAND, command_type, '/', index);
  1031. } else {
  1032. var recentComand = genericCreateCommand(command_type);
  1033. var index = function_obj.commands.indexOf($(el).data('command'));
  1034. if (index > -1) {
  1035. function_obj.commands.splice((index + 1), 0, recentComand);
  1036. }
  1037. renderCommand(recentComand, el, 2, function_obj);
  1038. registerUserEvent(function_obj.name, ActionTypes.INSERT_COMMAND, command_type, '/', index);
  1039. }
  1040. }
  1041. function findNearbyCommandToAddInFunctionScope (el, event, node_list_commands, function_obj, command_type) {
  1042. var all_sub = $(node_list_commands).find('div.command_container');
  1043. var menor_distancia = 999999999;
  1044. var elemento_menor_distancia = null;
  1045. var antes = true;
  1046. var t_bot;
  1047. var t_top;
  1048. // Descobrindo o elemento mais próximo:
  1049. for (var i = 0; i < all_sub.length; i++) {
  1050. t_top = all_sub[i].getBoundingClientRect().top;
  1051. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  1052. if ((t_top - event.clientY) < menor_distancia) {
  1053. menor_distancia = event.clientY - t_top;
  1054. elemento_menor_distancia = all_sub[i];
  1055. }
  1056. }
  1057. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  1058. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  1059. if ((borda_inferior - event.clientY) < menor_distancia) {
  1060. var recentComand = genericCreateCommand(command_type);
  1061. function_obj.commands.push(recentComand);
  1062. //
  1063. renderCommand(recentComand, node_list_commands, 3, function_obj);
  1064. registerUserEvent(function_obj.name, ActionTypes.INSERT_COMMAND, command_type, '/', function_obj.commands.length - 1);
  1065. } else {
  1066. var recentComand = genericCreateCommand(command_type);
  1067. var index = function_obj.commands.indexOf($(elemento_menor_distancia).data('command'));
  1068. if (index > -1) {
  1069. function_obj.commands.splice(index, 0, recentComand);
  1070. }
  1071. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  1072. registerUserEvent(function_obj.name, ActionTypes.INSERT_COMMAND, command_type, '/', index);
  1073. }
  1074. }