generic_expression.js 31 KB

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