1
0

commands.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. var has_element_created_draged = false;
  19. var which_element_is_draged = null;
  20. export function removeCommand (command, function_obj, dom_obj) {
  21. if (function_obj.commands.indexOf(command) > -1) {
  22. function_obj.commands.splice(function_obj.commands.indexOf(command), 1);
  23. return true;
  24. }
  25. // Utilize dois parantNode, pois o primeiro é o div de comandos
  26. if ($(dom_obj[0].parentNode.parentNode).data('command').commands_block.indexOf(command) > -1) {
  27. $(dom_obj[0].parentNode.parentNode).data('command').commands_block.splice
  28. ($(dom_obj[0].parentNode.parentNode).data('command').commands_block.indexOf(command), 1);
  29. return true;
  30. }
  31. return false;
  32. }
  33. export function createFloatingCommand (function_obj, function_container, command_type, mouse_event) {
  34. var floatingObject;
  35. switch (command_type) {
  36. case Models.COMMAND_TYPES.comment:
  37. floatingObject = CommentsManagement.createFloatingCommand();
  38. break;
  39. case Models.COMMAND_TYPES.reader:
  40. floatingObject = ReadersManagement.createFloatingCommand();
  41. break;
  42. case Models.COMMAND_TYPES.writer:
  43. floatingObject = WritersManagement.createFloatingCommand();
  44. break;
  45. case Models.COMMAND_TYPES.attribution:
  46. floatingObject = AttributionsManagement.createFloatingCommand();
  47. break;
  48. case Models.COMMAND_TYPES.iftrue:
  49. floatingObject = IftruesManagement.createFloatingCommand();
  50. break;
  51. case Models.COMMAND_TYPES.repeatNtimes:
  52. floatingObject = RepeatNtimesManagement.createFloatingCommand();
  53. break;
  54. case Models.COMMAND_TYPES.whiletrue:
  55. floatingObject = WhiletruesManagement.createFloatingCommand();
  56. break;
  57. case Models.COMMAND_TYPES.dowhiletrue:
  58. floatingObject = DowhiletruesManagement.createFloatingCommand();
  59. break;
  60. case Models.COMMAND_TYPES.switch:
  61. floatingObject = SwitchesManagement.createFloatingCommand();
  62. break;
  63. case Models.COMMAND_TYPES.functioncall:
  64. floatingObject = FunctioncallsManagement.createFloatingCommand();
  65. break;
  66. }
  67. floatingObject.draggable().appendTo("body");
  68. floatingObject.mouseup(function(evt) {
  69. manageCommand(function_obj, function_container, evt, command_type);
  70. });
  71. floatingObject.css("position", "absolute");
  72. mouse_event.type = "mousedown.draggable";
  73. mouse_event.target = floatingObject[0];
  74. floatingObject.css("left", mouse_event.pageX - 15);
  75. floatingObject.css("top", mouse_event.pageY - 15);
  76. floatingObject.trigger(mouse_event);
  77. }
  78. // before_after_inside: 1 -> before, 2 -> after, 3 -> inside
  79. export function renderCommand (command, element_reference, before_after_inside, function_obj) {
  80. var createdElement;
  81. switch (command.type) {
  82. case Models.COMMAND_TYPES.comment:
  83. createdElement = CommentsManagement.renderCommand(command, function_obj);
  84. break;
  85. case Models.COMMAND_TYPES.reader:
  86. createdElement = ReadersManagement.renderCommand(command, function_obj);
  87. break;
  88. case Models.COMMAND_TYPES.writer:
  89. createdElement = WritersManagement.renderCommand(command, function_obj);
  90. break;
  91. case Models.COMMAND_TYPES.attribution:
  92. createdElement = AttributionsManagement.renderCommand(command, function_obj);
  93. break;
  94. case Models.COMMAND_TYPES.functioncall:
  95. createdElement = FunctioncallsManagement.renderCommand(command, function_obj);
  96. break;
  97. case Models.COMMAND_TYPES.iftrue:
  98. createdElement = IftruesManagement.renderCommand(command, function_obj);
  99. break;
  100. case Models.COMMAND_TYPES.repeatNtimes:
  101. createdElement = RepeatNtimesManagement.renderCommand(command, function_obj);
  102. break;
  103. case Models.COMMAND_TYPES.whiletrue:
  104. createdElement = WhiletruesManagement.renderCommand(command, function_obj);
  105. break;
  106. case Models.COMMAND_TYPES.dowhiletrue:
  107. createdElement = DowhiletruesManagement.renderCommand(command, function_obj);
  108. break;
  109. case Models.COMMAND_TYPES.switch:
  110. createdElement = SwitchesManagement.renderCommand(command, function_obj);
  111. break;
  112. }
  113. switch (before_after_inside) {
  114. case 1:
  115. createdElement.insertBefore(element_reference);
  116. break;
  117. case 2:
  118. createdElement.insertAfter(element_reference);
  119. break;
  120. case 3:
  121. element_reference.append(createdElement);
  122. break;
  123. }
  124. }
  125. export function genericCreateCommand (command_type) {
  126. switch (command_type) {
  127. case Models.COMMAND_TYPES.comment:
  128. return new Models.Comment(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_value, LocalizedStrings.getUI('text_comment'), null, null, false));
  129. case Models.COMMAND_TYPES.reader:
  130. return new Models.Reader(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false));
  131. case Models.COMMAND_TYPES.writer:
  132. return new Models.Writer([new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  133. case Models.COMMAND_TYPES.attribution:
  134. return new Models.Attribution(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false),
  135. []);
  136. case Models.COMMAND_TYPES.functioncall:
  137. return new Models.FunctionCall(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_function, null, null, null, false), null);
  138. case Models.COMMAND_TYPES.iftrue:
  139. return new Models.IfTrue(null, null, null);
  140. case Models.COMMAND_TYPES.repeatNtimes:
  141. return new Models.RepeatNTimes(null, null, null, null);
  142. case Models.COMMAND_TYPES.whiletrue:
  143. return new Models.WhileTrue(null, null);
  144. case Models.COMMAND_TYPES.dowhiletrue:
  145. return new Models.DoWhileTrue(null, null);
  146. case Models.COMMAND_TYPES.switch:
  147. return new Models.Switch(null, null, null);
  148. }
  149. }
  150. function manageCommand (function_obj, function_container, event, command_type) {
  151. $( ".created_element" ).each(function( index ) {
  152. $(this).remove();
  153. });
  154. var el = $(document.elementFromPoint(event.clientX, event.clientY));
  155. console.log('soltou no: ');
  156. console.log(el);
  157. console.log(el.data('fun'));
  158. // Primeiro verificar se ele soltou no espaço da função correta:
  159. var hier = el.parentsUntil(".all_functions");
  160. var esta_correto = false;
  161. var esta_na_div_correta = false;
  162. if (el.hasClass("commands_list_div")) {
  163. esta_na_div_correta = true;
  164. }
  165. for (var i = 0; i < hier.length; i++) {
  166. var temp = $(hier[i]);
  167. if (temp.hasClass("commands_list_div")) {
  168. esta_na_div_correta = true;
  169. }
  170. if (temp.data('fun') == function_obj) {
  171. esta_correto = true;
  172. break;
  173. }
  174. }
  175. if (!esta_correto) {
  176. has_element_created_draged = false;
  177. which_element_is_draged = null;
  178. return;
  179. } else {
  180. if (!esta_na_div_correta) {
  181. has_element_created_draged = false;
  182. which_element_is_draged = null;
  183. return;
  184. }
  185. }
  186. // Agora é descobrir qual o escopo para adicionar o comando:
  187. // Se o elemento clicado possuir o atributo "fun", então, é direto na div dos comandos:
  188. if (typeof el.data('fun') !== 'undefined') {
  189. // Se a lista de comandos estiver vazia, então é o primeiro.
  190. // Portanto, ele deve soltar o elemento obrigatoriamente no objeto vazio
  191. if ((el.data('fun').commands == null) || (el.data('fun').commands.length == 0)) {
  192. // pode adicionar
  193. el.data('fun').commands = [];
  194. var new_cmd = genericCreateCommand(command_type);
  195. el.data('fun').commands.push(new_cmd);
  196. renderCommand(new_cmd, $(function_container).find('.commands_list_div'), 3, function_obj);
  197. } else { // Entra nesse else, caso já existam outros comandos no bloco:
  198. findNearbyCommandToAddInFunctionScope(el, event, $(function_container).find('.commands_list_div'), function_obj, command_type);
  199. }
  200. } else {
  201. console.log("soltou em um comando");
  202. // descobrir em qual comando ele soltou:
  203. var hier_find = el.parentsUntil(".commands_list_div");
  204. var hierarquia_bottom_up = [];
  205. if (typeof el.data('command') !== 'undefined') {
  206. hierarquia_bottom_up.push(el.data('command'));
  207. }
  208. for (var i = 0; i < hier_find.length; i++) {
  209. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  210. hierarquia_bottom_up.push($(hier_find[i]).data('command'));
  211. }
  212. }
  213. console.log("comando em que soltou: ");
  214. console.log(hierarquia_bottom_up[0]);
  215. console.log("hierarquia de baixo para cima na árvore, de onde ele soltou: ");
  216. for (var i = 0; i < hierarquia_bottom_up.length; i++) {
  217. console.log(hierarquia_bottom_up[i]);
  218. }
  219. // se a hierarquia possuir apenas um elemento, então está na raiz dos comandos:
  220. if (hierarquia_bottom_up.length == 1) {
  221. var sub_elemento = false;
  222. for (var i = 0; i < hier_find.length; i++) {
  223. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  224. findBeforeOrAfterCommandToAdd(hier_find[i], event, function_obj, command_type);
  225. sub_elemento = true;
  226. break;
  227. }
  228. }
  229. if (!sub_elemento) {
  230. findBeforeOrAfterCommandToAdd(el[0], event, function_obj, command_type);
  231. }
  232. } else {
  233. // caso exista mais de um elemento na hierarquia:
  234. if (typeof $(el).data('command') !== 'undefined') {
  235. console.log("PPP1");
  236. insertCommandInBlockHierar(el[0], event, function_obj, command_type, hier_find, hierarquia_bottom_up);
  237. } else {
  238. var sub_elemento = false;
  239. for (var i = 0; i < hier_find.length; i++) {
  240. if (typeof $(hier_find[i]).data('command') !== 'undefined') {
  241. insertCommandInBlockHierar(hier_find[i], event, function_obj, command_type, hier_find, hierarquia_bottom_up);
  242. sub_elemento = true;
  243. break;
  244. }
  245. }
  246. }
  247. }
  248. }
  249. has_element_created_draged = false;
  250. which_element_is_draged = null;
  251. }
  252. function insertCommandInBlockHierar (el, event, function_obj, command_type, hier_dom, hier_obj) {
  253. var el_jq = $(el);
  254. var command_parent = el_jq.data('command');
  255. if ((el_jq.data('command').type == Models.COMMAND_TYPES.repeatNtimes) ||
  256. (el_jq.data('command').type == Models.COMMAND_TYPES.whiletrue) ||
  257. (el_jq.data('command').type == Models.COMMAND_TYPES.dowhiletrue) ||
  258. (el_jq.data('command').type == Models.COMMAND_TYPES.iftrue) ||
  259. (el_jq.data('command').type == Models.COMMAND_TYPES.switch) ) {
  260. if ((el_jq.data('command').type == Models.COMMAND_TYPES.repeatNtimes) ||
  261. (el_jq.data('command').type == Models.COMMAND_TYPES.whiletrue) ||
  262. (el_jq.data('command').type == Models.COMMAND_TYPES.dowhiletrue) ) {
  263. // Se não tiver outro comando ainda no bloco, só adiciona:
  264. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  265. command_parent.commands_block = [];
  266. var recentComand = genericCreateCommand(command_type);
  267. command_parent.commands_block.push(recentComand);
  268. renderCommand(recentComand, el_jq.find('.block_commands'), 3, function_obj);
  269. } else { // Se já tem algum comando no bloco:
  270. findNearbyCommandToAddInBlockScope(el, event, el, function_obj, command_type, command_parent);
  271. }
  272. } else {
  273. // QUANDO FOR BLOCO DO TIPO IF OU SWITCH/CASE:
  274. }
  275. } else {
  276. // entra neste bloco, se soltou o comando sobre outro comando dentro de um subbloco:
  277. findBeforeOrAfterCommandToAddInsertBlock(el, event, function_obj, command_type);
  278. }
  279. }
  280. function findNearbyCommandToAddInBlockScope (el, event, node_list_commands, function_obj, command_type, command_parent) {
  281. var all_sub = $(node_list_commands).find('div.command_container');
  282. var menor_distancia = 999999999;
  283. var elemento_menor_distancia = null;
  284. var antes = true;
  285. var t_bot;
  286. var t_top;
  287. // Descobrindo o elemento mais próximo:
  288. for (var i = 0; i < all_sub.length; i++) {
  289. t_top = all_sub[i].getBoundingClientRect().top;
  290. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  291. if ((t_top - event.clientY) < menor_distancia) {
  292. menor_distancia = event.clientY - t_top;
  293. elemento_menor_distancia = all_sub[i];
  294. }
  295. }
  296. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  297. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  298. if ((borda_inferior - event.clientY) < menor_distancia) {
  299. var recentComand = genericCreateCommand(command_type);
  300. command_parent.commands_block.push(recentComand);
  301. //
  302. renderCommand(recentComand, node_list_commands, 3, function_obj);
  303. } else {
  304. var recentComand = genericCreateCommand(command_type);
  305. var index = command_parent.commands_block.indexOf($(elemento_menor_distancia).data('command'));
  306. if (index > -1) {
  307. command_parent.commands_block.splice(index, 0, recentComand);
  308. }
  309. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  310. }
  311. }
  312. function findBeforeOrAfterCommandToAddInsertBlock (el, event, function_obj, command_type) {
  313. var el_jq = $(el);
  314. var command_parent = $(el.parentNode.parentNode).data('command');
  315. var command_target = el_jq.data('command');
  316. var menor_distancia = 999999999;
  317. var antes = true;
  318. var t_bot;
  319. var t_top;
  320. t_top = el.getBoundingClientRect().top;
  321. t_bot = el.getBoundingClientRect().top + el.getBoundingClientRect().height;
  322. var d_top = event.clientY - t_top; // distancia topo
  323. var d_bot = t_bot - event.clientY; // distancia baixo
  324. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  325. if (d_top < d_bot) {
  326. var recentComand = genericCreateCommand(command_type);
  327. var index = command_parent.commands_block.indexOf(command_target);
  328. if (index > -1) {
  329. command_parent.commands_block.splice(index, 0, recentComand);
  330. }
  331. renderCommand(recentComand, el, 1, function_obj);
  332. } else {
  333. var recentComand = genericCreateCommand(command_type);
  334. var index = command_parent.commands_block.indexOf(command_target);
  335. if (index > -1) {
  336. command_parent.commands_block.splice((index + 1), 0, recentComand);
  337. }
  338. renderCommand(recentComand, el, 2, function_obj);
  339. }
  340. }
  341. function insertCommandInBlock (el, event, function_obj, command_type) {
  342. var el_jq = $(el);
  343. var command_parent = el_jq.data('command');
  344. if ((el_jq.data('command').type == Models.COMMAND_TYPES.repeatNtimes) ||
  345. (el_jq.data('command').type == Models.COMMAND_TYPES.whiletrue) ||
  346. (el_jq.data('command').type == Models.COMMAND_TYPES.dowhiletrue) ) {
  347. // Se não tiver outro comando ainda no bloco, só adiciona:
  348. if (command_parent.commands_block == null || command_parent.commands_block.length == 0) {
  349. command_parent.commands_block = [];
  350. var recentComand = genericCreateCommand(command_type);
  351. command_parent.commands_block.push(recentComand);
  352. renderCommand(recentComand, el_jq.find('.block_commands'), 3, function_obj);
  353. } else { // Se já tem algum comando no bloco:
  354. findInBlockCorrectPlace(el, event, function_obj, command_type);
  355. }
  356. } else {
  357. console.log("PPP2");
  358. }
  359. }
  360. function findInBlockCorrectPlace (el, event, function_obj, command_type) {
  361. var el_jq = $(el);
  362. var all_sub = el_jq.find('div.command_container');
  363. var menor_distancia = 999999999;
  364. var elemento_menor_distancia = null;
  365. var antes = true;
  366. var t_bot;
  367. var t_top;
  368. // Descobrindo o elemento mais próximo:
  369. for (var i = 0; i < all_sub.length; i++) {
  370. t_top = all_sub[i].getBoundingClientRect().top;
  371. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  372. if ((t_top - event.clientY) < menor_distancia) {
  373. menor_distancia = event.clientY - t_top;
  374. elemento_menor_distancia = all_sub[i];
  375. }
  376. }
  377. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  378. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  379. if ((borda_inferior - event.clientY) < menor_distancia) {
  380. var recentComand = genericCreateCommand(command_type);
  381. var command_parent = el_jq.data('command');
  382. command_parent.commands_block.push(recentComand);
  383. renderCommand(recentComand, $(el_jq.find('.block_commands')[0]), 3, function_obj);
  384. } else {
  385. var recentComand = genericCreateCommand(command_type);
  386. var command_parent = el_jq.data('command');
  387. var index = command_parent.commands_block.indexOf($(elemento_menor_distancia).data('command'));
  388. if (index > -1) {
  389. command_parent.commands_block.splice(index, 0, recentComand);
  390. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  391. }
  392. }
  393. }
  394. function findBeforeOrAfterCommandToAdd (el, event, function_obj, command_type) {
  395. switch ($(el).data('command').type) {
  396. case Models.COMMAND_TYPES.iftrue:
  397. case Models.COMMAND_TYPES.switch:
  398. case Models.COMMAND_TYPES.repeatNtimes:
  399. case Models.COMMAND_TYPES.whiletrue:
  400. case Models.COMMAND_TYPES.dowhiletrue:
  401. insertCommandInBlock(el, event, function_obj, command_type);
  402. return;
  403. }
  404. var menor_distancia = 999999999;
  405. var antes = true;
  406. var t_bot;
  407. var t_top;
  408. t_top = el.getBoundingClientRect().top;
  409. t_bot = el.getBoundingClientRect().top + el.getBoundingClientRect().height;
  410. var d_top = event.clientY - t_top; // distancia topo
  411. var d_bot = t_bot - event.clientY; // distancia baixo
  412. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  413. if (d_top < d_bot) {
  414. var recentComand = genericCreateCommand(command_type);
  415. var index = function_obj.commands.indexOf($(el).data('command'));
  416. if (index > -1) {
  417. function_obj.commands.splice(index, 0, recentComand);
  418. }
  419. renderCommand(recentComand, el, 1, function_obj);
  420. } else {
  421. var recentComand = genericCreateCommand(command_type);
  422. var index = function_obj.commands.indexOf($(el).data('command'));
  423. if (index > -1) {
  424. function_obj.commands.splice((index + 1), 0, recentComand);
  425. }
  426. renderCommand(recentComand, el, 2, function_obj);
  427. }
  428. }
  429. function findNearbyCommandToAddInFunctionScope (el, event, node_list_commands, function_obj, command_type) {
  430. var all_sub = $(node_list_commands).find('div.command_container');
  431. var menor_distancia = 999999999;
  432. var elemento_menor_distancia = null;
  433. var antes = true;
  434. var t_bot;
  435. var t_top;
  436. // Descobrindo o elemento mais próximo:
  437. for (var i = 0; i < all_sub.length; i++) {
  438. t_top = all_sub[i].getBoundingClientRect().top;
  439. t_bot = all_sub[i].getBoundingClientRect().top + all_sub[i].getBoundingClientRect().height;
  440. if ((t_top - event.clientY) < menor_distancia) {
  441. menor_distancia = event.clientY - t_top;
  442. elemento_menor_distancia = all_sub[i];
  443. }
  444. }
  445. var borda_inferior = elemento_menor_distancia.parentNode.getBoundingClientRect().top + elemento_menor_distancia.parentNode.getBoundingClientRect().height;
  446. // Está mais próximo da borda de baixo, ou seja.. inserir por último:
  447. if ((borda_inferior - event.clientY) < menor_distancia) {
  448. var recentComand = genericCreateCommand(command_type);
  449. function_obj.commands.push(recentComand);
  450. //
  451. renderCommand(recentComand, node_list_commands, 3, function_obj);
  452. } else {
  453. var recentComand = genericCreateCommand(command_type);
  454. var index = function_obj.commands.indexOf($(elemento_menor_distancia).data('command'));
  455. if (index > -1) {
  456. function_obj.commands.splice(index, 0, recentComand);
  457. }
  458. renderCommand(recentComand, elemento_menor_distancia, 1, function_obj);
  459. }
  460. }