commands.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. import $ from 'jquery';
  2. import { Types } from './types';
  3. import * as Models from './ivprog_elements';
  4. import { LocalizedStrings } from './../services/localizedStringsService';
  5. import * as GlobalsManagement from './globals';
  6. import * as VariablesManagement from './variables';
  7. import * as CommentsManagement from './commands/comment';
  8. import * as ReadersManagement from './commands/reader';
  9. import * as WritersManagement from './commands/writer';
  10. import * as AttributionsManagement from './commands/attribution';
  11. import * as IftruesManagement from './commands/iftrue';
  12. import * as RepeatNtimesManagement from './commands/repeatNtimes';
  13. import * as WhiletruesManagement from './commands/whiletrue';
  14. import * as DowhiletruesManagement from './commands/dowhiletrue';
  15. import * as SwitchesManagement from './commands/switch';
  16. import * as FunctioncallsManagement from './commands/functioncall';
  17. import * as VariableValueMenuManagement from './commands/variable_value_menu';
  18. import * as BreaksManagement from './commands/break';
  19. import * as ReturnsManagement from './commands/return';
  20. var has_element_created_draged = false;
  21. var which_element_is_draged = null;
  22. export function removeCommand (command, function_obj, dom_obj) {
  23. if (function_obj.commands.indexOf(command) > -1) {
  24. function_obj.commands.splice(function_obj.commands.indexOf(command), 1);
  25. return true;
  26. }
  27. // Utilize dois parantNode, pois o primeiro é o div de comandos
  28. try {
  29. if (dom_obj.parent().parent().data('command').commands_block.indexOf(command) > -1) {
  30. dom_obj.parent().parent().data('command').commands_block.splice
  31. (dom_obj.parent().parent().data('command').commands_block.indexOf(command), 1);
  32. return true;
  33. }
  34. } catch (err) {}
  35. try {
  36. if (dom_obj.parent().parent().data('command').type == Models.COMMAND_TYPES.iftrue) {
  37. if (dom_obj.parent().parent().data('command').commands_else.indexOf(command) > -1) {
  38. dom_obj.parent().parent().data('command').commands_else.splice
  39. (dom_obj.parent().parent().data('command').commands_else.indexOf(command), 1);
  40. return true;
  41. }
  42. }
  43. } catch (err) {}
  44. if (dom_obj.parent().data('switchcase')) {
  45. console.log("o que encontrei: ");
  46. console.log(dom_obj.parent().data('switchcase'));
  47. dom_obj.parent().data('switchcase').commands_block.splice(dom_obj.parent().data('switchcase').commands_block.indexOf(command), 1);
  48. return true;
  49. }
  50. return false;
  51. }
  52. export function createFloatingCommand (function_obj, function_container, command_type, mouse_event) {
  53. var floatingObject;
  54. switch (command_type) {
  55. case Models.COMMAND_TYPES.break:
  56. floatingObject = BreaksManagement.createFloatingCommand();
  57. break;
  58. case Models.COMMAND_TYPES.comment:
  59. floatingObject = CommentsManagement.createFloatingCommand();
  60. break;
  61. case Models.COMMAND_TYPES.reader:
  62. floatingObject = ReadersManagement.createFloatingCommand();
  63. break;
  64. case Models.COMMAND_TYPES.writer:
  65. floatingObject = WritersManagement.createFloatingCommand();
  66. break;
  67. case Models.COMMAND_TYPES.attribution:
  68. floatingObject = AttributionsManagement.createFloatingCommand();
  69. break;
  70. case Models.COMMAND_TYPES.iftrue:
  71. floatingObject = IftruesManagement.createFloatingCommand();
  72. break;
  73. case Models.COMMAND_TYPES.repeatNtimes:
  74. floatingObject = RepeatNtimesManagement.createFloatingCommand();
  75. break;
  76. case Models.COMMAND_TYPES.whiletrue:
  77. floatingObject = WhiletruesManagement.createFloatingCommand();
  78. break;
  79. case Models.COMMAND_TYPES.dowhiletrue:
  80. floatingObject = DowhiletruesManagement.createFloatingCommand();
  81. break;
  82. case Models.COMMAND_TYPES.switch:
  83. floatingObject = SwitchesManagement.createFloatingCommand();
  84. break;
  85. case Models.COMMAND_TYPES.functioncall:
  86. floatingObject = FunctioncallsManagement.createFloatingCommand();
  87. break;
  88. case Models.COMMAND_TYPES.return:
  89. floatingObject = ReturnsManagement.createFloatingCommand();
  90. break;
  91. }
  92. floatingObject.draggable({
  93. drag: function(evt) {
  94. borderMouseDragCommand(function_obj, function_container, evt);
  95. },
  96. stop: function(evt) {
  97. function_container.find('.over_command_drag').each(function( index ) {
  98. $(this).removeClass('over_command_drag');
  99. });
  100. }
  101. }).appendTo("body");
  102. floatingObject.mouseup(function(evt) {
  103. manageCommand(function_obj, function_container, evt, command_type);
  104. });
  105. floatingObject.css("position", "absolute");
  106. mouse_event.type = "mousedown.draggable";
  107. mouse_event.target = floatingObject[0];
  108. floatingObject.css("left", mouse_event.pageX - 15);
  109. floatingObject.css("top", mouse_event.pageY - 15);
  110. floatingObject.trigger(mouse_event);
  111. }
  112. function borderMouseDragCommand (function_obj, function_container, evt) {
  113. function_container.find('.over_command_drag').each(function( index ) {
  114. $(this).removeClass('over_command_drag');
  115. });
  116. var prev = null;
  117. function_container.find('.commands_list_div').each(function( index ) {
  118. prev = $(this);
  119. if (prev) {
  120. var objLeft = prev.offset().left;
  121. var objTop = prev.offset().top;
  122. var objRight = objLeft + prev.width();
  123. var objBottom = objTop + prev.height();
  124. if (evt.pageX > objLeft && evt.pageX < objRight && evt.pageY > objTop && evt.pageY < objBottom) {
  125. prev.addClass("over_command_drag");
  126. }
  127. }
  128. });
  129. function_container.find('.command_container').each(function( index ) {
  130. var obj = $(this);
  131. var objLeft = obj.offset().left;
  132. var objTop = obj.offset().top;
  133. var objRight = objLeft + obj.width();
  134. var objBottom = objTop + obj.height();
  135. if (evt.pageX > objLeft && evt.pageX < objRight && evt.pageY > objTop && evt.pageY < objBottom) {
  136. if (prev) {
  137. prev.removeClass('over_command_drag');
  138. }
  139. obj.addClass("over_command_drag");
  140. return;
  141. }
  142. });
  143. }
  144. // before_after_inside: 1 -> before, 2 -> after, 3 -> inside
  145. export function renderCommand (command, element_reference, before_after_inside, function_obj) {
  146. var createdElement;
  147. switch (command.type) {
  148. case Models.COMMAND_TYPES.comment:
  149. createdElement = CommentsManagement.renderCommand(command, function_obj);
  150. break;
  151. case Models.COMMAND_TYPES.break:
  152. createdElement = BreaksManagement.renderCommand(command, function_obj);
  153. break;
  154. case Models.COMMAND_TYPES.reader:
  155. createdElement = ReadersManagement.renderCommand(command, function_obj);
  156. break;
  157. case Models.COMMAND_TYPES.writer:
  158. createdElement = WritersManagement.renderCommand(command, function_obj);
  159. break;
  160. case Models.COMMAND_TYPES.attribution:
  161. createdElement = AttributionsManagement.renderCommand(command, function_obj);
  162. break;
  163. case Models.COMMAND_TYPES.functioncall:
  164. createdElement = FunctioncallsManagement.renderCommand(command, function_obj);
  165. break;
  166. case Models.COMMAND_TYPES.iftrue:
  167. createdElement = IftruesManagement.renderCommand(command, function_obj);
  168. break;
  169. case Models.COMMAND_TYPES.repeatNtimes:
  170. createdElement = RepeatNtimesManagement.renderCommand(command, function_obj);
  171. break;
  172. case Models.COMMAND_TYPES.whiletrue:
  173. createdElement = WhiletruesManagement.renderCommand(command, function_obj);
  174. break;
  175. case Models.COMMAND_TYPES.dowhiletrue:
  176. createdElement = DowhiletruesManagement.renderCommand(command, function_obj);
  177. break;
  178. case Models.COMMAND_TYPES.switch:
  179. createdElement = SwitchesManagement.renderCommand(command, function_obj);
  180. break;
  181. case Models.COMMAND_TYPES.return:
  182. createdElement = ReturnsManagement.renderCommand(command, function_obj);
  183. break;
  184. }
  185. switch (before_after_inside) {
  186. case 1:
  187. createdElement.insertBefore(element_reference);
  188. break;
  189. case 2:
  190. createdElement.insertAfter(element_reference);
  191. break;
  192. case 3:
  193. element_reference.append(createdElement);
  194. break;
  195. }
  196. }
  197. export function genericCreateCommand (command_type) {
  198. switch (command_type) {
  199. case Models.COMMAND_TYPES.break:
  200. return new Models.Break();
  201. case Models.COMMAND_TYPES.comment:
  202. return new Models.Comment(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_value, LocalizedStrings.getUI('text_comment'), null, null, false));
  203. case Models.COMMAND_TYPES.reader:
  204. return new Models.Reader(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false));
  205. case Models.COMMAND_TYPES.writer:
  206. return new Models.Writer([new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  207. case Models.COMMAND_TYPES.attribution:
  208. return new Models.Attribution(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false),
  209. []);
  210. case Models.COMMAND_TYPES.functioncall:
  211. return new Models.FunctionCall(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_function, null, null, null, false), null);
  212. case Models.COMMAND_TYPES.iftrue:
  213. return new Models.IfTrue(new Models.ConditionalExpression(null), null, null);
  214. case Models.COMMAND_TYPES.repeatNtimes:
  215. return new Models.RepeatNTimes(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false),
  216. new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false),
  217. null, new Models.ConditionalExpression(null), null, null);
  218. case Models.COMMAND_TYPES.whiletrue:
  219. return new Models.WhileTrue(new Models.ConditionalExpression(null), null);
  220. case Models.COMMAND_TYPES.dowhiletrue:
  221. return new Models.DoWhileTrue(new Models.ConditionalExpression(null), null);
  222. case Models.COMMAND_TYPES.switch:
  223. var sc = [new Models.SwitchCase(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true))];
  224. return new Models.Switch(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.variable_and_function, null, null, null, true), sc);
  225. case Models.COMMAND_TYPES.return:
  226. return new Models.Return(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true));
  227. }
  228. }
  229. function dragTrash (event) {
  230. var trash = $('<i class="ui icon trash alternate outline"></i>');
  231. $('body').append(trash);
  232. trash.css('position', 'absolute');
  233. trash.css('top', event.clientY);
  234. trash.css('left', event.clientX - 20);
  235. trash.css('font-size', '3em');
  236. trash.css('display', 'none');
  237. trash.fadeIn( 200, function() {
  238. trash.fadeOut( 200, function() {
  239. trash.remove();
  240. } );
  241. });
  242. }
  243. function manageCommand (function_obj, function_container, event, command_type) {
  244. $( ".created_element" ).each(function( index ) {
  245. $(this).remove();
  246. });
  247. var el = $(document.elementFromPoint(event.clientX, event.clientY));
  248. console.log('soltou no: ');
  249. console.log(el);
  250. console.log(el.data('fun'));
  251. // Primeiro verificar se ele soltou no espaço da função correta:
  252. var hier = el.parentsUntil(".all_functions");
  253. var esta_correto = false;
  254. var esta_na_div_correta = false;
  255. if (el.hasClass("commands_list_div")) {
  256. esta_na_div_correta = true;
  257. }
  258. for (var i = 0; i < hier.length; i++) {
  259. var temp = $(hier[i]);
  260. if (temp.hasClass("commands_list_div")) {
  261. esta_na_div_correta = true;
  262. }
  263. if (temp.data('fun') == function_obj) {
  264. esta_correto = true;
  265. break;
  266. }
  267. }
  268. if (!esta_correto) {
  269. has_element_created_draged = false;
  270. which_element_is_draged = null;
  271. dragTrash(event);
  272. return;
  273. } else {
  274. if (!esta_na_div_correta) {
  275. has_element_created_draged = false;
  276. which_element_is_draged = null;
  277. dragTrash(event);
  278. return;
  279. }
  280. }
  281. // Agora é descobrir qual o escopo para adicionar o comando:
  282. // Se o elemento clicado possuir o atributo "fun", então, é direto na div dos comandos:
  283. if (typeof el.data('fun') !== 'undefined') {
  284. // Se a lista de comandos estiver vazia, então é o primeiro.
  285. // Portanto, ele deve soltar o elemento obrigatoriamente no objeto vazio
  286. if ((el.data('fun').commands == null) || (el.data('fun').commands.length == 0)) {
  287. // pode adicionar
  288. el.data('fun').commands = [];
  289. var new_cmd = genericCreateCommand(command_type);
  290. el.data('fun').commands.push(new_cmd);
  291. renderCommand(new_cmd, $(function_container).find('.commands_list_div'), 3, function_obj);
  292. } else { // Entra nesse else, caso já existam outros comandos no bloco:
  293. findNearbyCommandToAddInFunctionScope(el, event, $(function_container).find('.commands_list_div'), function_obj, command_type);
  294. }
  295. } else {
  296. console.log("soltou em um comando");
  297. // descobrir em qual comando ele soltou:
  298. var hier_find = el.parentsUntil(".commands_list_div");
  299. var hierarquia_bottom_up = [];
  300. if (typeof el.data('command') !== 'undefined') {
  301. hierarquia_bottom_up.push(el.data('command'));
  302. }
  303. for (var i = 0; i < hier_find.length; i++) {
  304. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  305. hierarquia_bottom_up.push($(hier_find[i]).data('command'));
  306. }
  307. }
  308. console.log("comando em que soltou: ");
  309. console.log(hierarquia_bottom_up[0]);
  310. console.log("hierarquia de baixo para cima na árvore, de onde ele soltou: ");
  311. for (var i = 0; i < hierarquia_bottom_up.length; i++) {
  312. console.log(hierarquia_bottom_up[i]);
  313. }
  314. // Se for do tipo break, verificar se está no contexto correto:
  315. // Caso não esteja no contexto, apenas retorna sem dar continuidade:
  316. var is_correct_context = false;
  317. if (command_type == Models.COMMAND_TYPES.break) {
  318. for (var i = 0; i < hierarquia_bottom_up.length; i++) {
  319. if ((hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.repeatNtimes)
  320. || (hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.whiletrue)
  321. || (hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.dowhiletrue)
  322. || (hierarquia_bottom_up[i].type == Models.COMMAND_TYPES.switch)) {
  323. is_correct_context = true;
  324. break;
  325. }
  326. }
  327. if (!is_correct_context) {
  328. console.error("Context not allowed to insert BREAK COMMAND!");
  329. return;
  330. }
  331. }
  332. // se a hierarquia possuir apenas um elemento, então está na raiz dos comandos:
  333. if (hierarquia_bottom_up.length == 1) {
  334. console.log('QQ1');
  335. var sub_elemento = false;
  336. for (var i = 0; i < hier_find.length; i++) {
  337. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  338. console.log('QQ2');
  339. findBeforeOrAfterCommandToAdd(hier_find[i], event, function_obj, command_type);
  340. sub_elemento = true;
  341. break;
  342. }
  343. }
  344. if (!sub_elemento) {
  345. console.log('QQ3');
  346. findBeforeOrAfterCommandToAdd(el[0], event, function_obj, command_type);
  347. }
  348. } else {
  349. console.log('QQ4');
  350. // caso exista mais de um elemento na hierarquia:
  351. if (typeof $(el).data('command') !== 'undefined') {
  352. console.log('QQ5');
  353. console.log("PPP1");
  354. insertCommandInBlockHierar(el[0], event, function_obj, command_type, hier_find, hierarquia_bottom_up);
  355. } else {
  356. console.log('QQ6');
  357. var sub_elemento = false;
  358. for (var i = 0; i < hier_find.length; i++) {
  359. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  360. console.log('QQ7');
  361. insertCommandInBlockHierar(hier_find[i], event, function_obj, command_type, hier_find, hierarquia_bottom_up);
  362. sub_elemento = true;
  363. break;
  364. }
  365. }
  366. }
  367. }
  368. }
  369. has_element_created_draged = false;
  370. which_element_is_draged = null;
  371. renderAlgorithm();
  372. }
  373. function insertCommandInBlockHierar (el, event, function_obj, command_type, hier_dom, hier_obj) {
  374. var el_jq = $(el);
  375. var command_parent = el_jq.data('command');
  376. if ((el_jq.data('command').type == Models.COMMAND_TYPES.repeatNtimes) ||
  377. (el_jq.data('command').type == Models.COMMAND_TYPES.whiletrue) ||
  378. (el_jq.data('command').type == Models.COMMAND_TYPES.dowhiletrue) ||
  379. (el_jq.data('command').type == Models.COMMAND_TYPES.switch) ) {
  380. console.log('QQ17');
  381. if ((el_jq.data('command').type == Models.COMMAND_TYPES.repeatNtimes) ||
  382. (el_jq.data('command').type == Models.COMMAND_TYPES.whiletrue) ||
  383. (el_jq.data('command').type == Models.COMMAND_TYPES.dowhiletrue) ) {
  384. console.log('QQ18');
  385. // Se não tiver outro comando ainda no bloco, só adiciona:
  386. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  387. command_parent.commands_block = [];
  388. var recentComand = genericCreateCommand(command_type);
  389. command_parent.commands_block.push(recentComand);
  390. renderCommand(recentComand, el_jq.find('.block_commands'), 3, function_obj);
  391. } else { // Se já tem algum comando no bloco:
  392. findNearbyCommandToAddInBlockScope(el, event, el, function_obj, command_type, command_parent);
  393. }
  394. } else {
  395. // QUANDO FOR BLOCO DO TIPO IF OU SWITCH/CASE:
  396. addCommandToSwitchCase(event, function_obj, command_type);
  397. }
  398. } else {
  399. console.log('QQ19');
  400. // entra neste bloco, se soltou o comando sobre outro comando dentro de um subbloco:
  401. findBeforeOrAfterCommandToAddInsertBlock(el, event, function_obj, command_type);
  402. }
  403. }
  404. function findNearbyCommandToAddInBlockScope (el, event, node_list_commands, function_obj, command_type, command_parent) {
  405. var all_sub = $(node_list_commands).find('div.command_container');
  406. var menor_distancia = 999999999;
  407. var elemento_menor_distancia = null;
  408. var antes = true;
  409. var t_bot;
  410. var t_top;
  411. // Descobrindo o elemento mais próximo:
  412. for (var i = 0; i < all_sub.length; i++) {
  413. t_top = all_sub[i].getBoundingClientRect().top;
  414. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  415. if ((t_top - event.clientY) < menor_distancia) {
  416. menor_distancia = event.clientY - t_top;
  417. elemento_menor_distancia = all_sub[i];
  418. }
  419. }
  420. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  421. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  422. if ((borda_inferior - event.clientY) < menor_distancia) {
  423. var recentComand = genericCreateCommand(command_type);
  424. command_parent.commands_block.push(recentComand);
  425. //
  426. renderCommand(recentComand, node_list_commands, 3, function_obj);
  427. } else {
  428. var recentComand = genericCreateCommand(command_type);
  429. var index = command_parent.commands_block.indexOf($(elemento_menor_distancia).data('command'));
  430. if (index > -1) {
  431. command_parent.commands_block.splice(index, 0, recentComand);
  432. }
  433. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  434. }
  435. }
  436. function findBeforeOrAfterCommandToAddInsertBlock (el, event, function_obj, command_type) {
  437. var el_jq = $(el);
  438. var command_parent = $(el.parentNode.parentNode).data('command');
  439. var command_target = el_jq.data('command');
  440. var temp_parent = $(el.parentNode.parentNode);
  441. var is_in_else = false;
  442. if (!command_parent) {
  443. command_parent = el_jq.data('command');
  444. temp_parent = el_jq;
  445. var hier = el_jq.parentsUntil(".command_container");
  446. for (var i = 0; i < hier.length; i++) {
  447. var temp = $(hier[i]);
  448. if (typeof temp.data('else') != 'undefined') {
  449. is_in_else = true;
  450. }
  451. if (typeof temp.data('command') != 'undefined') {
  452. command_parent = temp.data('command');
  453. temp_parent = temp;
  454. }
  455. }
  456. }
  457. var hier = el_jq.parentsUntil(".command_container");
  458. for (var i = 0; i < hier.length; i++) {
  459. var temp = $(hier[i]);
  460. if (typeof temp.data('else') != 'undefined') {
  461. is_in_else = true;
  462. }
  463. }
  464. if (command_parent == command_target) {
  465. var hier = el_jq.parentsUntil(".command_container");
  466. for (var i = 0; i < hier.length; i++) {
  467. var temp = $(hier[i]);
  468. if (typeof temp.data('else') !== 'undefined') {
  469. is_in_else = true;
  470. break;
  471. }
  472. }
  473. }
  474. if ((command_parent.type != Models.COMMAND_TYPES.iftrue) && (command_parent.type != Models.COMMAND_TYPES.switch)) {
  475. var hier = temp_parent.parentsUntil(".all_cases_div");
  476. console.log("vou procurar!!");
  477. for (var i = 0; i < hier.length; i++) {
  478. console.log("estou vasculhando...");
  479. var temp = $(hier[i]);
  480. if (typeof temp.data('switchcase') !== 'undefined') {
  481. console.log("encontrei");
  482. command_parent = temp.data('switchcase');
  483. is_in_else = false;
  484. break;
  485. }
  486. }
  487. }
  488. console.log('debugging:');
  489. console.log('el_jq');
  490. console.log(el_jq);
  491. console.log('command_parent');
  492. console.log(command_parent);
  493. console.log('command_target');
  494. console.log(command_target);
  495. var menor_distancia = 999999999;
  496. var antes = true;
  497. var t_bot;
  498. var t_top;
  499. t_top = el.getBoundingClientRect().top;
  500. t_bot = el.getBoundingClientRect().top + el.getBoundingClientRect().height;
  501. var d_top = event.clientY - t_top; // distancia topo
  502. var d_bot = t_bot - event.clientY; // distancia baixo
  503. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  504. if (d_top < d_bot) {
  505. var recentComand = genericCreateCommand(command_type);
  506. console.log('MMM1');
  507. if (is_in_else) {
  508. console.log('MMM2');
  509. if (command_parent == command_target) {
  510. console.log('MMM3');
  511. if (command_parent.commands_else == null || command_parent.commands_else.length == 0) {
  512. command_parent.commands_else = [];
  513. var recentComand = genericCreateCommand(command_type);
  514. command_parent.commands_else.push(recentComand);
  515. renderCommand(recentComand, el_jq, 3, function_obj);
  516. } else { // Se já tem algum comando no bloco:
  517. findInBlockCorrectPlace(el_jq, event, function_obj, command_type, true);
  518. }
  519. return;
  520. }
  521. console.log('MMM7');
  522. var index = command_parent.commands_else.indexOf(command_target);
  523. if (index > -1) {
  524. command_parent.commands_else.splice(index, 0, recentComand);
  525. }
  526. renderCommand(recentComand, el, 1, function_obj);
  527. } else {
  528. console.log('MMM4');
  529. if (command_parent == command_target) {
  530. console.log('Nxxxx5');
  531. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  532. command_parent.commands_block = [];
  533. console.log('SSS4');
  534. var recentComand = genericCreateCommand(command_type);
  535. command_parent.commands_block.push(recentComand);
  536. renderCommand(recentComand, el_jq, 3, function_obj);
  537. } else {
  538. console.log('SSS5');
  539. findInBlockCorrectPlace(el_jq, event, function_obj, command_type);
  540. }
  541. return;
  542. }
  543. console.log('MMM6');
  544. var index = command_parent.commands_block.indexOf(command_target);
  545. if (index > -1) {
  546. command_parent.commands_block.splice(index, 0, recentComand);
  547. }
  548. renderCommand(recentComand, el, 1, function_obj);
  549. }
  550. } else {
  551. console.log('XXX1');
  552. var recentComand = genericCreateCommand(command_type);
  553. if (is_in_else) {
  554. if (command_parent == command_target) {
  555. console.log('MMM3');
  556. if (command_parent.commands_else == null || command_parent.commands_else.length == 0) {
  557. command_parent.commands_else = [];
  558. console.log('SSS1');
  559. var recentComand = genericCreateCommand(command_type);
  560. command_parent.commands_else.push(recentComand);
  561. renderCommand(recentComand, el_jq, 3, function_obj);
  562. } else { // Se já tem algum comando no bloco:
  563. console.log('SSS2');
  564. findInBlockCorrectPlace(el_jq, event, function_obj, command_type, true);
  565. }
  566. return;
  567. }
  568. console.log('XXX2');
  569. var index = command_parent.commands_else.indexOf(command_target);
  570. if (index > -1) {
  571. command_parent.commands_else.splice((index + 1), 0, recentComand);
  572. }
  573. renderCommand(recentComand, el, 2, function_obj);
  574. } else {
  575. if (command_parent == command_target) {
  576. console.log('Nxxxx78');
  577. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  578. command_parent.commands_block = [];
  579. var recentComand = genericCreateCommand(command_type);
  580. command_parent.commands_block.push(recentComand);
  581. console.log('SSS6');
  582. renderCommand(recentComand, el_jq, 3, function_obj);
  583. } else {
  584. console.log('SSS7');
  585. findInBlockCorrectPlace(el_jq, event, function_obj, command_type);
  586. }
  587. return;
  588. }
  589. console.log('XXX3');
  590. var index = command_parent.commands_block.indexOf(command_target);
  591. if (index > -1) {
  592. command_parent.commands_block.splice((index + 1), 0, recentComand);
  593. }
  594. renderCommand(recentComand, el, 2, function_obj);
  595. }
  596. }
  597. }
  598. function insertCommandInBlock (el, event, function_obj, command_type) {
  599. var el_jq = $(el);
  600. var command_parent = el_jq.data('command');
  601. if ((el_jq.data('command').type == Models.COMMAND_TYPES.repeatNtimes) ||
  602. (el_jq.data('command').type == Models.COMMAND_TYPES.whiletrue) ||
  603. (el_jq.data('command').type == Models.COMMAND_TYPES.dowhiletrue) ) {
  604. // Se não tiver outro comando ainda no bloco, só adiciona:
  605. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  606. command_parent.commands_block = [];
  607. var recentComand = genericCreateCommand(command_type);
  608. command_parent.commands_block.push(recentComand);
  609. renderCommand(recentComand, el_jq.find('.block_commands'), 3, function_obj);
  610. } else { // Se já tem algum comando no bloco:
  611. findInBlockCorrectPlace(el, event, function_obj, command_type);
  612. }
  613. } else if (el_jq.data('command').type == Models.COMMAND_TYPES.iftrue) {
  614. console.log('QQ9');
  615. // no if ou no else?
  616. var correct_div = $(document.elementFromPoint(event.pageX, event.pageY));
  617. var is_in_if = true;
  618. if (correct_div.data('if')) {
  619. is_in_if = true;
  620. } else if (correct_div.data('else')) {
  621. is_in_if = false;
  622. } else {
  623. var hier = correct_div.parentsUntil(".command_container");
  624. for (var i = 0; i < hier.length; i++) {
  625. var temp = $(hier[i]);
  626. if (typeof temp.data('if') !== 'undefined') {
  627. is_in_if = true;
  628. break;
  629. }
  630. if (typeof temp.data('else') !== 'undefined') {
  631. is_in_if = false;
  632. break;
  633. }
  634. }
  635. }
  636. if (is_in_if) {
  637. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  638. command_parent.commands_block = [];
  639. var recentComand = genericCreateCommand(command_type);
  640. command_parent.commands_block.push(recentComand);
  641. renderCommand(recentComand, el_jq.find('.commands_if'), 3, function_obj);
  642. } else { // Se já tem algum comando no bloco:
  643. findInBlockCorrectPlace(el_jq.find('.commands_if'), event, function_obj, command_type);
  644. }
  645. } else {
  646. if (command_parent.commands_else == null || command_parent.commands_else.length == 0) {
  647. command_parent.commands_else = [];
  648. var recentComand = genericCreateCommand(command_type);
  649. command_parent.commands_else.push(recentComand);
  650. renderCommand(recentComand, el_jq.find('.commands_else'), 3, function_obj);
  651. } else { // Se já tem algum comando no bloco:
  652. findInBlockCorrectPlace(el_jq.find('.commands_else'), event, function_obj, command_type, true);
  653. }
  654. }
  655. } else { // é do tipo switch
  656. console.log("está tentando inserir em um switch que está na raiz!");
  657. addCommandToSwitchCase(event, function_obj, command_type);
  658. }
  659. }
  660. function addCommandToSwitchCase (event, function_obj, command_type) {
  661. var el = $(document.elementFromPoint(event.clientX, event.clientY));
  662. var which_case = el.data('switchcase');
  663. var case_div = el;
  664. if (!which_case) {
  665. var hier_find = el.parentsUntil(".all_cases_div");
  666. for (var i = 0; i < hier_find.length; i++) {
  667. if (typeof $(hier_find[i]).data('switchcase') !== 'undefined') {
  668. which_case = $(hier_find[i]).data('switchcase');
  669. case_div = $(hier_find[i]);
  670. break;
  671. }
  672. }
  673. }
  674. if (which_case.commands_block == null || which_case.commands_block.length < 1) {
  675. which_case.commands_block = [];
  676. var recentComand = genericCreateCommand(command_type);
  677. which_case.commands_block.push(recentComand);
  678. renderCommand(recentComand, case_div.find('.case_commands_block'), 3, function_obj);
  679. } else {
  680. findInBlockCorrectPlaceInSwitchCase(which_case, case_div, event, function_obj, command_type);
  681. }
  682. }
  683. function findInBlockCorrectPlaceInSwitchCase (which_case, case_div, event, function_obj, command_type) {
  684. var all_sub = case_div.find('div.command_container');
  685. var menor_distancia = 999999999;
  686. var elemento_menor_distancia = null;
  687. var antes = true;
  688. var t_bot;
  689. var t_top;
  690. // Descobrindo o elemento mais próximo:
  691. for (var i = 0; i < all_sub.length; i++) {
  692. t_top = all_sub[i].getBoundingClientRect().top;
  693. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  694. if ((t_top - event.clientY) < menor_distancia) {
  695. menor_distancia = event.clientY - t_top;
  696. elemento_menor_distancia = all_sub[i];
  697. }
  698. }
  699. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  700. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  701. if ((borda_inferior - event.clientY) < menor_distancia) {
  702. var recentComand = genericCreateCommand(command_type);
  703. which_case.commands_block.push(recentComand);
  704. renderCommand(recentComand, $(case_div.find('.case_commands_block')[0]), 3, function_obj);
  705. } else {
  706. var recentComand = genericCreateCommand(command_type);
  707. var index = which_case.commands_block.indexOf($(elemento_menor_distancia).data('command'));
  708. if (index > -1) {
  709. which_case.commands_block.splice(index, 0, recentComand);
  710. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  711. }
  712. }
  713. }
  714. function findInBlockCorrectPlace (el, event, function_obj, command_type, is_in_else = false) {
  715. var el_jq = $(el);
  716. var all_sub = el_jq.find('div.command_container');
  717. var menor_distancia = 999999999;
  718. var elemento_menor_distancia = null;
  719. var antes = true;
  720. var t_bot;
  721. var t_top;
  722. // Descobrindo o elemento mais próximo:
  723. for (var i = 0; i < all_sub.length; i++) {
  724. t_top = all_sub[i].getBoundingClientRect().top;
  725. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  726. if ((t_top - event.clientY) < menor_distancia) {
  727. menor_distancia = event.clientY - t_top;
  728. elemento_menor_distancia = all_sub[i];
  729. }
  730. }
  731. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  732. console.log("menor_distancia: ");
  733. console.log(elemento_menor_distancia);
  734. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  735. if ((borda_inferior - event.clientY) < menor_distancia) {
  736. console.log('QQ11');
  737. var recentComand = genericCreateCommand(command_type);
  738. var command_parent = el_jq.data('command');
  739. if (is_in_else) {
  740. console.log('QQ15');
  741. command_parent.commands_else.push(recentComand);
  742. console.log('el_jq');
  743. console.log(el_jq);
  744. console.log("$(el_jq.find('.commands_else')[0]):: ");
  745. console.log($(el_jq.find('.commands_else')[0]));
  746. renderCommand(recentComand, el_jq, 3, function_obj);
  747. } else {
  748. console.log('QQ16');
  749. command_parent.commands_block.push(recentComand);
  750. renderCommand(recentComand, $(el_jq.find('.block_commands')[0]), 3, function_obj);
  751. }
  752. } else {
  753. console.log('QQ12');
  754. var recentComand = genericCreateCommand(command_type);
  755. var command_parent = el_jq.data('command');
  756. if (is_in_else) {
  757. var index = command_parent.commands_else.indexOf($(elemento_menor_distancia).data('command'));
  758. if (index > -1) {
  759. command_parent.commands_else.splice(index, 0, recentComand);
  760. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  761. }
  762. } else {
  763. var index = command_parent.commands_block.indexOf($(elemento_menor_distancia).data('command'));
  764. if (index > -1) {
  765. command_parent.commands_block.splice(index, 0, recentComand);
  766. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  767. }
  768. }
  769. }
  770. }
  771. function findBeforeOrAfterCommandToAdd (el, event, function_obj, command_type) {
  772. switch ($(el).data('command').type) {
  773. case Models.COMMAND_TYPES.iftrue:
  774. case Models.COMMAND_TYPES.switch:
  775. case Models.COMMAND_TYPES.repeatNtimes:
  776. case Models.COMMAND_TYPES.whiletrue:
  777. case Models.COMMAND_TYPES.dowhiletrue:
  778. insertCommandInBlock(el, event, function_obj, command_type);
  779. return;
  780. }
  781. var menor_distancia = 999999999;
  782. var antes = true;
  783. var t_bot;
  784. var t_top;
  785. t_top = el.getBoundingClientRect().top;
  786. t_bot = el.getBoundingClientRect().top + el.getBoundingClientRect().height;
  787. var d_top = event.clientY - t_top; // distancia topo
  788. var d_bot = t_bot - event.clientY; // distancia baixo
  789. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  790. if (d_top < d_bot) {
  791. var recentComand = genericCreateCommand(command_type);
  792. var index = function_obj.commands.indexOf($(el).data('command'));
  793. if (index > -1) {
  794. function_obj.commands.splice(index, 0, recentComand);
  795. }
  796. renderCommand(recentComand, el, 1, function_obj);
  797. } else {
  798. var recentComand = genericCreateCommand(command_type);
  799. var index = function_obj.commands.indexOf($(el).data('command'));
  800. if (index > -1) {
  801. function_obj.commands.splice((index + 1), 0, recentComand);
  802. }
  803. renderCommand(recentComand, el, 2, function_obj);
  804. }
  805. }
  806. function findNearbyCommandToAddInFunctionScope (el, event, node_list_commands, function_obj, command_type) {
  807. var all_sub = $(node_list_commands).find('div.command_container');
  808. var menor_distancia = 999999999;
  809. var elemento_menor_distancia = null;
  810. var antes = true;
  811. var t_bot;
  812. var t_top;
  813. // Descobrindo o elemento mais próximo:
  814. for (var i = 0; i < all_sub.length; i++) {
  815. t_top = all_sub[i].getBoundingClientRect().top;
  816. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  817. if ((t_top - event.clientY) < menor_distancia) {
  818. menor_distancia = event.clientY - t_top;
  819. elemento_menor_distancia = all_sub[i];
  820. }
  821. }
  822. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  823. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  824. if ((borda_inferior - event.clientY) < menor_distancia) {
  825. var recentComand = genericCreateCommand(command_type);
  826. function_obj.commands.push(recentComand);
  827. //
  828. renderCommand(recentComand, node_list_commands, 3, function_obj);
  829. } else {
  830. var recentComand = genericCreateCommand(command_type);
  831. var index = function_obj.commands.indexOf($(elemento_menor_distancia).data('command'));
  832. if (index > -1) {
  833. function_obj.commands.splice(index, 0, recentComand);
  834. }
  835. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  836. }
  837. }