1
0

commands.js 34 KB

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