commands_sidebar.js 44 KB

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