generic_expression.js 30 KB

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