1
0

commands.js 33 KB

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