commands.js 32 KB

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