generic_expression.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. import { Types } from '../types';
  2. import * as Models from '../ivprog_elements';
  3. import { LocalizedStrings } from '../../services/localizedStringsService';
  4. import * as VariableValueMenuManagement from './variable_value_menu';
  5. import WatchJS from 'melanke-watchjs';
  6. export function renderExpression (command, function_obj, div_to_render, expression_array) {
  7. div_to_render.empty();
  8. if (command.type === Models.COMMAND_TYPES.attribution) {
  9. WatchJS.unwatch(command.variable);
  10. WatchJS.watch(command.variable, function(){
  11. renderExpression(command, function_obj, div_to_render, expression_array);
  12. }, 0);
  13. if (command.variable.content) {
  14. var types_included = [];
  15. if (command.variable.content.type == Types.INTEGER || command.variable.content.type == Types.REAL) {
  16. types_included.push(Models.EXPRESSION_TYPES.exp_arithmetic);
  17. } else if (command.variable.content.type == Types.BOOLEAN) {
  18. types_included.push(Models.EXPRESSION_TYPES.exp_conditional);
  19. types_included.push(Models.EXPRESSION_TYPES.exp_logic);
  20. types_included.push(Models.EXPRESSION_TYPES.exp_arithmetic);
  21. } else if (command.variable.content.type == Types.TEXT) {
  22. types_included.push(Models.EXPRESSION_TYPES.exp_conditional);
  23. types_included.push(Models.EXPRESSION_TYPES.exp_logic);
  24. types_included.push(Models.EXPRESSION_TYPES.exp_arithmetic);
  25. } else {
  26. console.log('NÃO RECONHECI! VEJA: ', command.variable.content.type);
  27. }
  28. renderElements(command, function_obj, div_to_render, expression_array, types_included);
  29. } else {
  30. div_to_render.text(LocalizedStrings.getUI('var_menu_select_var').toLowerCase());
  31. }
  32. } else {
  33. var types_included = [];
  34. types_included.push(Models.EXPRESSION_TYPES.exp_conditional);
  35. types_included.push(Models.EXPRESSION_TYPES.exp_logic);
  36. types_included.push(Models.EXPRESSION_TYPES.exp_arithmetic);
  37. renderElements(command, function_obj, div_to_render, expression_array, types_included);
  38. }
  39. div_to_render.children('.mouse_distance').addClass('mouse_distance_hidden');
  40. div_to_render.children('.higher_element').on('mousemove', function(evt) {
  41. if (!window.open_or_close) {
  42. $(this).css('position', 'relative', '!important');
  43. $(this).children('.mouse_distance').css('opacity', '1');
  44. }
  45. });
  46. div_to_render.children('.higher_element').on('mouseout', function(evt) {
  47. if (!window.open_or_close) {
  48. $(this).css('position', 'absolute', '!important');
  49. $(this).children('.mouse_distance').css('opacity', '0');
  50. }
  51. });
  52. var lixeira = $('<div class="lixeira" draggable="true"></div>');
  53. div_to_render.find('.single_element_expression').on('mousedown', function (evt) {
  54. window.posX = evt.clientX;
  55. window.posY = evt.clientY;
  56. });
  57. Sortable.create(div_to_render[0], {
  58. animation: 100,
  59. ghostClass: 'ghost',
  60. group: {
  61. name: 'shared',
  62. put: false // Do not allow items to be put into this list
  63. },
  64. draggable: '.single_element_expression',
  65. sort: false,
  66. filter: '.not_allowed',
  67. onStart: function() {
  68. $('body').append(lixeira);
  69. lixeira.css('display', 'block');
  70. lixeira.css('top', window.posY + 70, '!important');
  71. lixeira.css('left', window.posX - 20, '!important');
  72. },
  73. onMove: function() {
  74. lixeira.addClass('color_test');
  75. },
  76. onEnd: function() {
  77. lixeira.remove();
  78. div_to_render.find('.ghost').removeClass('ghost');
  79. }
  80. });
  81. new Sortable(lixeira[0], {
  82. group: 'shared',
  83. animation: 150,
  84. onAdd: function (evt) {
  85. lixeira.css('display', 'none');
  86. lixeira.find('.single_element_expression').remove();
  87. lixeira.css('background-color', '');
  88. lixeira.remove();
  89. removeElement(evt, expression_array);
  90. renderExpression(command, function_obj, div_to_render, expression_array);
  91. }
  92. });
  93. }
  94. function removeElement (event, expression_array) {
  95. var indice = $(event.item).data('index');
  96. var first = expression_array[0];
  97. console.log('indice: ', indice);
  98. if (expression_array[indice].type) {
  99. // if is alone in expression:
  100. if (expression_array.length == 1) {
  101. //function_obj.commands.splice(function_obj.commands.indexOf(command), 1);
  102. expression_array.splice(0, 1);
  103. } else if (expression_array.length > 1) {
  104. if (indice > 0 && expression_array[indice - 1].type_op) {
  105. if (indice < (expression_array.length)
  106. && expression_array[indice - 2] == '('
  107. && expression_array[indice + 1].type_op) {
  108. expression_array.splice(indice + 1, 1);
  109. }
  110. expression_array.splice(indice, 1);
  111. expression_array.splice(indice - 1, 1);
  112. if (indice - 2 < (expression_array.length)
  113. && expression_array[indice - 2] == '('
  114. && expression_array[indice - 1] == ')') {
  115. expression_array.splice(indice - 1, 1);
  116. expression_array.splice(indice - 2, 1);
  117. if (indice - 3 >= 0 && indice - 3 < expression_array.length
  118. && expression_array[indice - 3].type_op ) {
  119. expression_array.splice(indice - 3, 1);
  120. }
  121. }
  122. } else if (indice < (expression_array.length - 1) && expression_array[indice + 1].type_op) {
  123. expression_array.splice(indice + 1, 1);
  124. expression_array.splice(indice, 1);
  125. } else if (indice < (expression_array.length - 1) && indice > 0
  126. && expression_array[indice -1] == '(' && expression_array[indice +1] == ')') {
  127. if (indice > 1
  128. && expression_array[indice - 2].type_op) {
  129. expression_array.splice(indice + 1, 1);
  130. expression_array.splice(indice, 1);
  131. expression_array.splice(indice - 1, 1);
  132. expression_array.splice(indice - 2, 1);
  133. } else if (indice < (expression_array.length - 2)
  134. && expression_array[indice + 2].type_op) {
  135. expression_array.splice(indice + 1, 1);
  136. expression_array.splice(indice, 1);
  137. expression_array.splice(indice - 1, 1);
  138. } else {
  139. expression_array.splice(indice + 1, 1);
  140. expression_array.splice(indice, 1);
  141. expression_array.splice(indice - 1, 1);
  142. }
  143. }
  144. }
  145. } else if (expression_array[indice].type_op) {
  146. // iVProg doesn't support operator remove
  147. } else {
  148. var opening = -1;
  149. var closing = -1;
  150. if (expression_array[indice] == '(') {
  151. opening = indice;
  152. for (var i = indice + 1; i < expression_array.length; i++) {
  153. if (expression_array[i] == ')') {
  154. closing = i;
  155. break;
  156. }
  157. }
  158. } else {
  159. closing = indice;
  160. for (var i = indice - 1; i >= 0; i--) {
  161. if (expression_array[i] == '(') {
  162. opening = i;
  163. break;
  164. }
  165. }
  166. }
  167. if (expression_array[opening + 1].type_op) {
  168. expression_array.splice(closing, 1);
  169. expression_array.splice(opening + 1, 1);
  170. expression_array.splice(opening, 1);
  171. } else {
  172. expression_array.splice(closing, 1);
  173. expression_array.splice(opening, 1);
  174. }
  175. }
  176. // if expression is empty, add a new var value:
  177. if (expression_array.length == 0) {
  178. expression_array.push(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true));
  179. }
  180. if (first != expression_array[0] && expression_array[0].type_op) {
  181. expression_array.splice(0, 1);
  182. }
  183. }
  184. function renderElements (command, function_obj, div_to_render, expression_array, types_included) {
  185. /*if (expression_array.length > 0) {
  186. if (!expression_array[0].type_op) {
  187. renderStartAddOperator(div_to_render, types_included, expression_array, command, function_obj, 0);
  188. }
  189. }*/
  190. var i = 0;
  191. for (i = 0; i < expression_array.length; i++) {
  192. if (expression_array[i].type == "var_value") {
  193. var div_temp = $('<div class="single_element_expression" data-index="'+i+'"></div>');
  194. if (i == 0) {
  195. if (expression_array.length > 0 && !expression_array[0].type_op) {
  196. renderStartAddOperator(div_to_render, types_included, expression_array, command, function_obj, 0);
  197. }
  198. }
  199. VariableValueMenuManagement.renderMenu(command, expression_array[i], div_temp, function_obj);
  200. div_to_render.append(div_temp);
  201. } else if (expression_array[i] == '(' || expression_array[i] == ')') {
  202. if (expression_array[i] == ')') {
  203. renderFinalAddElements(div_to_render, types_included, expression_array, command, function_obj, i);
  204. renderParenthesis(div_to_render, expression_array[i], command, function_obj, i, expression_array);
  205. } else if (expression_array[i] == '(' && !expression_array[i + 1].type_op) {
  206. renderParenthesis(div_to_render, expression_array[i], command, function_obj, i, expression_array);
  207. renderStartAddOperator(div_to_render, types_included, expression_array, command, function_obj, i + 1);
  208. } else {
  209. renderParenthesis(div_to_render, expression_array[i], command, function_obj, i, expression_array);
  210. }
  211. } else {
  212. if (i == 0) {
  213. console.log("NEGAÇÃO NO PRIMEIRO ELEMENTO");
  214. } else if (expression_array[i - 1] == '(') {
  215. console.log("NEGAÇÃO APÓS O PARÊNTESES");
  216. }
  217. renderOperatorMenu(command, function_obj, div_to_render, expression_array[i], types_included, i, expression_array);
  218. }
  219. }
  220. renderFinalAddElements(div_to_render, types_included, expression_array, command, function_obj, i, true);
  221. renderAddParenthesis(command, function_obj, div_to_render, expression_array, types_included);
  222. }
  223. window.parentheses_activate = false;
  224. window.open_or_close = null;
  225. function renderAddParenthesis (command, function_obj, div_to_render, expression_array, types_included) {
  226. var addParentheses = $('<div class="single_element_expression add_parentheses not_allowed"><i class="icons"><b style="font-style: normal;">( )</b><i class="corner add icon blue" style="font-size: .6em;right: -3px;bottom: -2px;"></i></i></div>');
  227. div_to_render.append(addParentheses);
  228. addParentheses.popup({
  229. content : "Adicionar parênteses",
  230. delay: {
  231. show: 750,
  232. hide: 0
  233. }
  234. });
  235. addParentheses.on('click', function(mouse_event) {
  236. // verificar se já está ativado
  237. if (window.parentheses_activate) {
  238. return;
  239. }
  240. div_to_render.find('.usepointer').off('click');
  241. window.parentheses_activate = true;
  242. window.open_or_close = "open";
  243. div_to_render.find('.dropdown').addClass('disabled');
  244. div_to_render.find('.ghost_element').addClass('temp_class');
  245. div_to_render.find('.ghost_element').removeClass('ghost_element');
  246. var floatingObject = $('<div class="floating_parenthesis"> ( </div>');
  247. floatingObject.draggable().appendTo("body");
  248. floatingObject.css("position", "absolute");
  249. mouse_event.type = "mousedown.draggable";
  250. mouse_event.target = floatingObject[0];
  251. floatingObject.css("left", mouse_event.pageX + 10);
  252. floatingObject.css("top", mouse_event.pageY + 10);
  253. floatingObject.trigger(mouse_event);
  254. div_to_render.on('mousemove', function(evt) {
  255. var actual_target = null;
  256. if ($(evt.target).hasClass('single_element_expression')) {
  257. actual_target = $(evt.target);
  258. } else {
  259. actual_target = $(evt.target).closest('.single_element_expression');
  260. }
  261. if ($(evt.target).hasClass('temp_class')
  262. || actual_target.length < 1
  263. || actual_target.hasClass('add_parentheses')
  264. || actual_target.hasClass('rendered_parentheses')
  265. || $(evt.target).hasClass('expression_elements')) {
  266. return;
  267. }
  268. renderGhostParentheses(actual_target, command, function_obj, div_to_render, expression_array);
  269. });
  270. div_to_render.on('mouseleave', function() {
  271. /*window.open_parentheses.remove();
  272. window.close_parentheses.remove();*/
  273. });
  274. var floating;
  275. $('body').on('mouseup', function(evt) {
  276. if (window.open_or_close == "open") {
  277. window.open_or_close = "close";
  278. floatingObject.remove();
  279. var comando_que_esta = $(evt.target).closest('.command_container');
  280. var comando_certo = div_to_render.closest('.command_container');
  281. if (!comando_que_esta.is(comando_certo)) {
  282. window.parentheses_activate = false;
  283. div_to_render.find('.temp_class').addClass('ghost_element');
  284. div_to_render.find('.temp_class').removeClass('temp_class');
  285. div_to_render.off('mousemove');
  286. div_to_render.off('mouseleave');
  287. $('body').off('mouseup');
  288. window.open_parentheses.remove();
  289. window.close_parentheses.remove();
  290. window.inserir_open = -1;
  291. window.inserir_close = -1;
  292. window.open_or_close = null;
  293. renderExpression(command, function_obj, div_to_render, expression_array);
  294. return;
  295. }
  296. window.open_parentheses.addClass('parentheses_fixed');
  297. floating = $('<div class="floating_parenthesis"> ) </div>');
  298. floating.draggable().appendTo("body");
  299. floating.css("position", "absolute");
  300. floating.css("left", evt.pageX + 10);
  301. floating.css("top", evt.pageY + 10);
  302. $('body').on('mousemove', function(evts) {
  303. floating.css("left", evts.pageX + 10);
  304. floating.css("top", evts.pageY + 10);
  305. });
  306. } else {
  307. floating.remove();
  308. window.open_parentheses.removeClass('parentheses_fixed');
  309. div_to_render.off('mousemove');
  310. div_to_render.off('mouseleave');
  311. $('body').off('mouseup');
  312. setTimeout(function(){
  313. window.parentheses_activate = false;
  314. }, 50);
  315. var comando_que_esta = $(evt.target).closest('.command_container');
  316. var comando_certo = div_to_render.closest('.command_container');
  317. var is_correct = false;
  318. if (comando_que_esta.is(comando_certo)) {
  319. is_correct = true;
  320. }
  321. if (is_correct) {
  322. expression_array.splice(window.inserir_open, 0, '(');
  323. expression_array.splice(window.inserir_close, 0, ')');
  324. }
  325. window.inserir_open = -1;
  326. window.inserir_close = -1;
  327. window.open_or_close = null;
  328. renderExpression(command, function_obj, div_to_render, expression_array);
  329. }
  330. });
  331. });
  332. }
  333. window.open_parentheses = $('<div class="parentheses_ghost">(</div>');
  334. window.close_parentheses = $('<div class="parentheses_ghost">)</div>');
  335. window.inserir_open = -1;
  336. window.inserir_close = -1;
  337. function renderGhostParentheses (actual_target, command, function_obj, div_to_render, expression_array) {
  338. /*window.open_parentheses.remove();
  339. window.close_parentheses.remove();*/
  340. var index_in_array = actual_target.data('index');
  341. if ((expression_array[index_in_array] == '(') || (expression_array[index_in_array] == ')')) {
  342. return;
  343. }
  344. if (window.open_or_close == "close") {
  345. if (index_in_array < window.inserir_open) {
  346. return;
  347. }
  348. }
  349. // Tratando a situação quando é na primeira posição:
  350. if (index_in_array == 0) {
  351. if (expression_array[index_in_array].type == "var_value") {
  352. if (window.open_or_close == "open") {
  353. window.open_parentheses.insertBefore(actual_target);
  354. window.inserir_open = index_in_array;
  355. }
  356. /*if (expression_array.length == 1) {
  357. if (window.open_or_close == "close") {
  358. window.close_parentheses.insertAfter(actual_target);
  359. window.inserir_close = index_in_array + 2;
  360. }*/
  361. //} else {
  362. var count_opened = 0;
  363. var count_closed = 0;
  364. for (var i = 0; i < expression_array.length; i++) {
  365. if ((expression_array[i] == '(')) {
  366. count_opened ++;
  367. }
  368. if (expression_array[i] == ')') {
  369. count_closed ++;
  370. }
  371. if (count_opened != count_closed) {
  372. } else {
  373. if (count_opened > 0) {
  374. if (window.open_or_close == "close") {
  375. window.close_parentheses.insertAfter(div_to_render.find('.single_element_expression[data-index="'+i+'"]'));
  376. window.inserir_close = i + 2;
  377. }
  378. break;
  379. } else {
  380. if (expression_array[i].type == "var_value") {
  381. if (window.open_or_close == "close") {
  382. window.close_parentheses.insertAfter(div_to_render.find('.single_element_expression[data-index="'+i+'"]'));
  383. window.inserir_close = i + 2;
  384. }
  385. break;
  386. }
  387. }
  388. }
  389. }
  390. //}
  391. } else if (expression_array[index_in_array].type_op) {
  392. if (window.open_or_close == "open") {
  393. window.open_parentheses.insertBefore(actual_target);
  394. window.inserir_open = index_in_array;
  395. }
  396. var count_opened = 0;
  397. var count_closed = 0;
  398. for (var i = 1; i < expression_array.length; i++) {
  399. // $('.slide-link[data-slide="0"]')
  400. if ((expression_array[i] == '(')) {
  401. count_opened ++;
  402. }
  403. if (expression_array[i] == ')') {
  404. count_closed ++;
  405. }
  406. if (count_opened != count_closed) {
  407. } else {
  408. if (count_opened > 0) {
  409. if (expression_array[i].type == "var_value") {
  410. window.close_parentheses.insertAfter(div_to_render.find('.single_element_expression[data-index="'+i+'"]'));
  411. window.inserir_close = i + 2;
  412. }
  413. break;
  414. } else {
  415. if (expression_array[i].type == "var_value") {
  416. if (expression_array[i].type == "var_value") {
  417. window.close_parentheses.insertAfter(div_to_render.find('.single_element_expression[data-index="'+i+'"]'));
  418. window.inserir_close = i + 2;
  419. }
  420. break;
  421. }
  422. }
  423. }
  424. }
  425. }
  426. return;
  427. }
  428. // Tratando quando não é no índice 0:
  429. if (expression_array[index_in_array].type == "var_value") {
  430. if (window.open_or_close == "open") {
  431. window.open_parentheses.insertBefore(actual_target);
  432. window.inserir_open = index_in_array;
  433. }
  434. if (window.open_or_close == "close") {
  435. window.close_parentheses.insertAfter(actual_target);
  436. window.inserir_close = index_in_array + 2;
  437. }
  438. return;
  439. }
  440. if (expression_array[index_in_array].type_op) {
  441. // buscar para a esquerda primeiro:
  442. if (expression_array[index_in_array - 1] == '(') {
  443. if (window.open_or_close == "open") {
  444. window.open_parentheses.insertBefore(actual_target);
  445. window.inserir_open = index_in_array;
  446. }
  447. } else if (expression_array[index_in_array - 1] == ')') {
  448. // buscar a abertura
  449. var count_opened = 0;
  450. var count_closed = 0;
  451. for (var j = index_in_array - 1; j >= 0; j--) {
  452. if ((expression_array[j] == '(')) {
  453. count_opened ++;
  454. }
  455. if (expression_array[j] == ')') {
  456. count_closed ++;
  457. }
  458. if (count_opened != count_closed) {
  459. } else {
  460. if (count_closed > 0) {
  461. if (window.open_or_close == "open") {
  462. window.open_parentheses.insertBefore(div_to_render.find('.single_element_expression[data-index="'+j+'"]'));
  463. window.inserir_open = j;
  464. }
  465. break;
  466. }
  467. }
  468. }
  469. } else if (expression_array[index_in_array - 1].type == "var_value") {
  470. if (window.open_or_close == "open") {
  471. window.open_parentheses.insertBefore(div_to_render.find('.single_element_expression[data-index="'+(index_in_array - 1)+'"]'));
  472. window.inserir_open = index_in_array - 1;
  473. }
  474. }
  475. // buscar para a direita agora:
  476. if (expression_array[index_in_array + 1] == '(') {
  477. // buscar o fechamento:
  478. var count_opened = 0;
  479. var count_closed = 0;
  480. for (var j = index_in_array + 1; j < expression_array.length; j ++) {
  481. if ((expression_array[j] == '(')) {
  482. count_opened ++;
  483. }
  484. if (expression_array[j] == ')') {
  485. count_closed ++;
  486. }
  487. if (count_opened != count_closed) {
  488. } else {
  489. if (count_opened > 0) {
  490. if (window.open_or_close == "close") {
  491. window.close_parentheses.insertAfter(div_to_render.find('.single_element_expression[data-index="'+j+'"]'));
  492. window.inserir_close = j + 2;
  493. }
  494. break;
  495. }
  496. }
  497. }
  498. } else if (expression_array[index_in_array + 1].type == "var_value") {
  499. if (window.open_or_close == "close") {
  500. window.close_parentheses.insertAfter(div_to_render.find('.single_element_expression[data-index="'+(index_in_array + 1)+'"]'));
  501. window.inserir_close = index_in_array + 3;
  502. }
  503. }
  504. }
  505. }
  506. function renderParenthesis (div_to_render, expression_content, command, function_obj, position, expression_array) {
  507. var ghost_parenthesis = $('<div class="single_element_expression parentheses_in_expression" data-index="'+position+'">'+expression_content+'</div>');
  508. div_to_render.append(ghost_parenthesis);
  509. }
  510. function renderStartAddOperator (div_to_render, types_included, expression_array, command, function_obj, position) {
  511. var menu_final = '<div class="ui dropdown disabled usepointer"><div class="text"> + </div><i class="dropdown icon"></i><div class="menu">';
  512. if (types_included.indexOf(Models.EXPRESSION_TYPES.exp_arithmetic) >= 0) {
  513. if (types_included.length > 1) {
  514. menu_final += '<div class="item"><i class="dropdown icon"></i>Aritméticos<div class="menu">';
  515. menu_final += getArithmeticOperators();
  516. menu_final += '</div></div>';
  517. } else {
  518. menu_final += getArithmeticOperators();
  519. }
  520. }
  521. if (types_included.indexOf(Models.EXPRESSION_TYPES.exp_logic) >= 0) {
  522. if (types_included.length > 1) {
  523. menu_final += '<div class="item"><i class="dropdown icon"></i>Lógicos<div class="menu">';
  524. menu_final += getLogicOperators();
  525. menu_final += '</div></div>';
  526. } else {
  527. menu_final += getLogicOperators();
  528. }
  529. }
  530. if (types_included.indexOf(Models.EXPRESSION_TYPES.exp_conditional) >= 0) {
  531. if (types_included.length > 1) {
  532. menu_final += '<div class="item"><i class="dropdown icon"></i>Relacionais<div class="menu">';
  533. menu_final += getRelationalOperators();
  534. menu_final += '</div></div>';
  535. } else {
  536. menu_final += getRelationalOperators();
  537. }
  538. }
  539. menu_final += '</div></div>';
  540. menu_final = $(menu_final);
  541. var div_temp = $('<div class="single_element_expression ghost_element mouse_distance"></div>');
  542. div_temp.append(menu_final);
  543. var div_higher = $('<div class="higher_element"></div>');
  544. div_higher.append(div_temp);
  545. div_to_render.append(div_higher);
  546. menu_final.dropdown('set selected', Models.ARITHMETIC_TYPES.minus);
  547. div_temp.on('click', function() {
  548. if (!window.open_or_close) {
  549. var sera = position;
  550. if (types_included.indexOf(Models.EXPRESSION_TYPES.exp_arithmetic) >= 0) {
  551. console.log('p1');
  552. expression_array.splice(sera, 0, new Models.ExpressionOperator(Models.EXPRESSION_TYPES.exp_arithmetic,Models.ARITHMETIC_TYPES.minus));
  553. } else if (types_included.indexOf(Models.EXPRESSION_TYPES.exp_logic) >= 0) {
  554. console.log('p2');
  555. expression_array.splice(sera, 0, new Models.ExpressionOperator(Models.EXPRESSION_TYPES.exp_logic,Models.LOGIC_COMPARISON.equals_to));
  556. } else if (types_included.indexOf(Models.EXPRESSION_TYPES.exp_conditional) >= 0) {
  557. console.log('p3');
  558. expression_array.splice(sera, 0, new Models.ExpressionOperator(Models.EXPRESSION_TYPES.exp_conditional,Models.ARITHMETIC_COMPARISON.greater_than));
  559. }
  560. renderExpression(command, function_obj, div_to_render, expression_array);
  561. }
  562. });
  563. }
  564. function renderFinalAddElements (div_to_render, types_included, expression_array, command, function_obj, position, is_last = false) {
  565. var menu_final = '<div class="ui dropdown disabled usepointer"><div class="text"> + </div><i class="dropdown icon"></i><div class="menu">';
  566. if (types_included.indexOf(Models.EXPRESSION_TYPES.exp_arithmetic) >= 0) {
  567. if (types_included.length > 1) {
  568. menu_final += '<div class="item"><i class="dropdown icon"></i>Aritméticos<div class="menu">';
  569. menu_final += getArithmeticOperators();
  570. menu_final += '</div></div>';
  571. } else {
  572. menu_final += getArithmeticOperators();
  573. }
  574. }
  575. if (types_included.indexOf(Models.EXPRESSION_TYPES.exp_logic) >= 0) {
  576. if (types_included.length > 1) {
  577. menu_final += '<div class="item"><i class="dropdown icon"></i>Lógicos<div class="menu">';
  578. menu_final += getLogicOperators();
  579. menu_final += '</div></div>';
  580. } else {
  581. menu_final += getLogicOperators();
  582. }
  583. }
  584. if (types_included.indexOf(Models.EXPRESSION_TYPES.exp_conditional) >= 0) {
  585. if (types_included.length > 1) {
  586. menu_final += '<div class="item"><i class="dropdown icon"></i>Relacionais<div class="menu">';
  587. menu_final += getRelationalOperators();
  588. menu_final += '</div></div>';
  589. } else {
  590. menu_final += getRelationalOperators();
  591. }
  592. }
  593. menu_final += '</div></div>';
  594. menu_final = $(menu_final);
  595. var div_temp = $('<div class="simple_add mouse_distance"></div>');
  596. var div_higher = $('<div class="higher_element"></div>');
  597. var button = $('<button class="ui button green add_expression"><i class="plus circle inverted icon"></i></button>');
  598. div_temp.append(button);
  599. if (!is_last) {
  600. div_higher.append(div_temp);
  601. div_to_render.append(div_higher);
  602. //div_temp.append(menu_final);
  603. div_temp.css('opacity', '0', '!important');
  604. } else {
  605. div_temp.removeClass('mouse_distance');
  606. div_temp.css('opacity', '1', '!important');
  607. //div_temp.append(menu_final);
  608. div_to_render.append(div_temp);
  609. }
  610. menu_final.dropdown('set selected', Models.ARITHMETIC_TYPES.plus);
  611. div_temp.on('click', function() {
  612. var sera = position;
  613. if (expression_array[sera] == ')' && expression_array[sera - 1] == '(') {
  614. expression_array.splice(sera, 0, new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true));
  615. renderExpression(command, function_obj, div_to_render, expression_array);
  616. return;
  617. }
  618. if (types_included.indexOf(Models.EXPRESSION_TYPES.exp_arithmetic) >= 0) {
  619. expression_array.splice(sera, 0, new Models.ExpressionOperator(Models.EXPRESSION_TYPES.exp_arithmetic,Models.ARITHMETIC_TYPES.plus));
  620. expression_array.splice(sera + 1, 0, new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true));
  621. } else if (types_included.indexOf(Models.EXPRESSION_TYPES.exp_logic) >= 0) {
  622. expression_array.splice(sera, 0, new Models.ExpressionOperator(Models.EXPRESSION_TYPES.exp_logic,Models.LOGIC_COMPARISON.equals_to));
  623. expression_array.splice(sera + 1, 0, new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true));
  624. } else if (types_included.indexOf(Models.EXPRESSION_TYPES.exp_conditional) >= 0) {
  625. expression_array.splice(sera, 0, new Models.ExpressionOperator(Models.EXPRESSION_TYPES.exp_conditional,Models.ARITHMETIC_COMPARISON.greater_than));
  626. expression_array.splice(sera + 1, 0, new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true));
  627. }
  628. renderExpression(command, function_obj, div_to_render, expression_array);
  629. });
  630. }
  631. function renderOperatorMenu (command, function_obj, div_to_render, expression_element, types_included, position, expression_array) {
  632. var menu_final = '<div class="ui dropdown"><div class="text"> + </div><i class="dropdown icon"></i><div class="menu">';
  633. if (types_included.indexOf(Models.EXPRESSION_TYPES.exp_arithmetic) >= 0) {
  634. if (types_included.length > 1) {
  635. menu_final += '<div class="item"><i class="dropdown icon"></i>Aritméticos<div class="menu">';
  636. menu_final += getArithmeticOperators();
  637. menu_final += '</div></div>';
  638. } else {
  639. menu_final += getArithmeticOperators();
  640. }
  641. }
  642. if (types_included.indexOf(Models.EXPRESSION_TYPES.exp_logic) >= 0) {
  643. if (types_included.length > 1) {
  644. menu_final += '<div class="item"><i class="dropdown icon"></i>Lógicos<div class="menu">';
  645. menu_final += getLogicOperators();
  646. menu_final += '</div></div>';
  647. } else {
  648. menu_final += getLogicOperators();
  649. }
  650. }
  651. if (types_included.indexOf(Models.EXPRESSION_TYPES.exp_conditional) >= 0) {
  652. if (types_included.length > 1) {
  653. menu_final += '<div class="item"><i class="dropdown icon"></i>Relacionais<div class="menu">';
  654. menu_final += getRelationalOperators();
  655. menu_final += '</div></div>';
  656. } else {
  657. menu_final += getRelationalOperators();
  658. }
  659. }
  660. menu_final += '</div></div>';
  661. menu_final = $(menu_final);
  662. var div_temp = $('<div class="single_element_expression not_allowed" data-index="'+position+'"></div>');
  663. div_temp.append(menu_final);
  664. div_to_render.append(div_temp);
  665. menu_final.dropdown({
  666. onChange: function(value, text, $selectedItem) {
  667. expression_element.item = $selectedItem.data('value');
  668. expression_element.type_op = $selectedItem.data('type');
  669. }
  670. });
  671. menu_final.dropdown('set selected', expression_element.item);
  672. }
  673. function getArithmeticOperators () {
  674. var arithmetic_operators;
  675. arithmetic_operators = '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_arithmetic+'" data-value="'+Models.ARITHMETIC_TYPES.plus+'">+</div>';
  676. arithmetic_operators += '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_arithmetic+'" data-value="'+Models.ARITHMETIC_TYPES.minus+'">-</div>';
  677. arithmetic_operators += '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_arithmetic+'" data-value="'+Models.ARITHMETIC_TYPES.multiplication+'">*</div>';
  678. arithmetic_operators += '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_arithmetic+'" data-value="'+Models.ARITHMETIC_TYPES.division+'">/</div>';
  679. arithmetic_operators += '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_arithmetic+'" data-value="'+Models.ARITHMETIC_TYPES.module+'">%</div>';
  680. return arithmetic_operators;
  681. }
  682. function getLogicOperators () {
  683. var logic_operators;
  684. logic_operators = '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_logic+'" data-value="'+Models.LOGIC_COMPARISON.equals_to+'">==</div>';
  685. logic_operators += '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_logic+'" data-value="'+Models.LOGIC_COMPARISON.not_equals_to+'">!=</div>';
  686. logic_operators += '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_logic+'" data-value="'+Models.LOGIC_COMPARISON.and+'">'+LocalizedStrings.getUI('and')+'</div>';
  687. logic_operators += '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_logic+'" data-value="'+Models.LOGIC_COMPARISON.or+'">'+LocalizedStrings.getUI('or')+'</div>';
  688. logic_operators += '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_logic+'" data-value="'+Models.LOGIC_COMPARISON.not+'">'+LocalizedStrings.getUI('not')+'</div>';
  689. return logic_operators;
  690. }
  691. function getRelationalOperators () {
  692. var relational_operators;
  693. relational_operators = '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_conditional+'" data-value="'+Models.ARITHMETIC_COMPARISON.greater_than+'">></div>';
  694. relational_operators += '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_conditional+'" data-value="'+Models.ARITHMETIC_COMPARISON.less_than+'"><</div>';
  695. relational_operators += '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_conditional+'" data-value="'+Models.ARITHMETIC_COMPARISON.equals_to+'">==</div>';
  696. relational_operators += '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_conditional+'" data-value="'+Models.ARITHMETIC_COMPARISON.not_equals_to+'">!=</div>';
  697. relational_operators += '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_conditional+'" data-value="'+Models.ARITHMETIC_COMPARISON.greater_than_or_equals_to+'">>=</div>';
  698. relational_operators += '<div class="item" data-type="'+Models.EXPRESSION_TYPES.exp_conditional+'" data-value="'+Models.ARITHMETIC_COMPARISON.less_than_or_equals_to+'"><=</div>';
  699. return relational_operators;
  700. }