commands_sidebar.js 42 KB

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