conditional_expression.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // iVProg - www.usp.br/line/ivprog
  2. // LInE - Free Education, Private Data
  3. import * as Models from '../ivprog_elements';
  4. import { LocalizedStrings } from '../../services/localizedStringsService';
  5. import * as VariableValueMenuManagement from './variable_value_menu';
  6. export function renderExpression (command, expression, function_obj, initial_el_to_render) {
  7. //D console.log("src/ivprog/js/visualUI/commands/conditional_expression.js: renderExpression(.): command=" +
  8. //D JSON.stringify(command) + "\nexpression=" + JSON.stringify(expression) + "\nfunction_obj=||" + JSON.stringify(function_obj) + "||\n");
  9. expression.dom_object = initial_el_to_render;
  10. if (expression.expression == null || expression.expression.length < 1) {
  11. renderStartMenu(command, expression, function_obj, initial_el_to_render);
  12. } else {
  13. var main_div = $('<div class="expression_elements"></div>');
  14. switch (expression.expression.type) {
  15. case Models.EXPRESSION_TYPES.exp_logic:
  16. renderLogicExpression(command, expression, expression.expression, function_obj, main_div, initial_el_to_render);
  17. break;
  18. case Models.EXPRESSION_TYPES.exp_arithmetic:
  19. renderArithmeticExpression(command, expression, expression.expression, function_obj, main_div);
  20. break;
  21. }
  22. initial_el_to_render.append(main_div);
  23. var restartMenu = $('<div class="ui restart_expression"><i class="ui icon undo"></i></div>');
  24. initial_el_to_render.append(restartMenu);
  25. restartMenu.on('click', function(e){
  26. expression.expression = null;
  27. initial_el_to_render.empty();
  28. renderExpression(command, expression, function_obj, initial_el_to_render);
  29. });
  30. }
  31. }
  32. function renderArithmeticOperator (command, all_expression, expression_arithmetic, arithmetic_operator, function_obj, element_to_append) {
  33. var menu_operator = $('<div class="ui dropdown"><div class="text"></div><i class="dropdown icon"></i></div>');
  34. menu_operator.dropdown({
  35. values: [
  36. { name : '>',
  37. value : Models.ARITHMETIC_COMPARISON.greater_than,
  38. selected : (arithmetic_operator == Models.ARITHMETIC_COMPARISON.greater_than)
  39. },
  40. { name : '<',
  41. value : Models.ARITHMETIC_COMPARISON.less_than,
  42. selected : (arithmetic_operator == Models.ARITHMETIC_COMPARISON.less_than)
  43. },
  44. { name : '==',
  45. value : Models.ARITHMETIC_COMPARISON.equals_to,
  46. selected : (arithmetic_operator == Models.ARITHMETIC_COMPARISON.equals_to)
  47. },
  48. { name : '!=',
  49. value : Models.ARITHMETIC_COMPARISON.not_equals_to,
  50. selected : (arithmetic_operator == Models.ARITHMETIC_COMPARISON.not_equals_to)
  51. },
  52. { name : '>=',
  53. value : Models.ARITHMETIC_COMPARISON.greater_than_or_equals_to,
  54. selected : (arithmetic_operator == Models.ARITHMETIC_COMPARISON.greater_than_or_equals_to)
  55. },
  56. { name : '<=',
  57. value : Models.ARITHMETIC_COMPARISON.less_than_or_equals_to,
  58. selected : (arithmetic_operator == Models.ARITHMETIC_COMPARISON.less_than_or_equals_to)
  59. }
  60. ],
  61. onChange: function(value, text, $selectedItem) {
  62. expression_arithmetic.operator = value;
  63. }
  64. });
  65. element_to_append.append(menu_operator);
  66. }
  67. function renderLogicOperator (command, all_expression, expression_logic, logic_operator, function_obj, element_to_append, initial_el_to_render) {
  68. var menu_operator = $('<div class="ui dropdown"><div class="text"></div><i class="dropdown icon"></i></div>');
  69. menu_operator.dropdown({
  70. values: [
  71. { name : '==',
  72. value : Models.LOGIC_COMPARISON.equals_to,
  73. selected : (logic_operator == Models.LOGIC_COMPARISON.equals_to)
  74. },
  75. { name : '!=',
  76. value : Models.LOGIC_COMPARISON.not_equals_to,
  77. selected : (logic_operator == Models.LOGIC_COMPARISON.not_equals_to)
  78. },
  79. { name : '&&',
  80. value : Models.LOGIC_COMPARISON.and,
  81. selected : (logic_operator == Models.LOGIC_COMPARISON.and)
  82. },
  83. { name : '||',
  84. value : Models.LOGIC_COMPARISON.or,
  85. selected : (logic_operator == Models.LOGIC_COMPARISON.or)
  86. }
  87. ],
  88. onChange: function(value, text, $selectedItem) {
  89. if ($selectedItem) {
  90. expression_logic.operator = value;
  91. if (expression_logic.second_operand == null) {
  92. expression_logic.second_operand = new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true);
  93. initial_el_to_render.empty();
  94. renderExpression(command, all_expression, function_obj, initial_el_to_render);
  95. }
  96. }
  97. }
  98. });
  99. element_to_append.append(menu_operator);
  100. }
  101. function renderLogicExpression (command, all_expression, expression_logic, function_obj, element_to_append, initial_el_to_render) {
  102. var exp_el_par_1 = $(' <span class="span_command_spec"> </span> ');
  103. var exp_el_expr_el_1 = $('<div class="expression_element"></div>');
  104. var exp_el_expr_operand = $('<div class="expression_element"></div>');
  105. var exp_el_expr_el_2 = $('<div class="expression_element"></div>');
  106. var exp_el_par_2 = $(' <span class="span_command_spec"> </span> ');
  107. if (expression_logic.first_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  108. renderLogicExpression(command, all_expression, expression_logic.first_operand, function_obj, exp_el_expr_el_1);
  109. } else if (expression_logic.first_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic) {
  110. renderArithmeticExpression(command, all_expression, expression_logic.first_operand, function_obj, exp_el_expr_el_1);
  111. } else {
  112. VariableValueMenuManagement.renderMenu(command, expression_logic.first_operand, exp_el_expr_el_1, function_obj);
  113. }
  114. element_to_append.append(exp_el_par_1);
  115. element_to_append.append(exp_el_expr_el_1);
  116. renderLogicOperator(command, all_expression, expression_logic, expression_logic.operator, function_obj, exp_el_expr_operand, initial_el_to_render);
  117. element_to_append.append(exp_el_expr_operand);
  118. if (expression_logic.second_operand) {
  119. if (expression_logic.second_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  120. renderLogicExpression(command, all_expression, expression_logic.second_operand, function_obj, exp_el_expr_el_2);
  121. } else if (expression_logic.second_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic) {
  122. renderArithmeticExpression(command, all_expression, expression_logic.second_operand, function_obj, exp_el_expr_el_2);
  123. } else {
  124. VariableValueMenuManagement.renderMenu(command, expression_logic.second_operand, exp_el_expr_el_2, function_obj);
  125. }
  126. element_to_append.append(exp_el_expr_el_2);
  127. }
  128. element_to_append.append(exp_el_par_2);
  129. }
  130. function renderArithmeticExpression (command, all_expression, expression_arithmetic, function_obj, element_to_append) {
  131. var exp_el_par_1 = $(' <span class="span_command_spec"> </span> ');
  132. var exp_el_expr_el_1 = $('<div class="expression_element"></div>');
  133. var exp_el_expr_operand = $('<div class="expression_element"></div>');
  134. var exp_el_expr_el_2 = $('<div class="expression_element"></div>');
  135. var exp_el_par_2 = $(' <span class="span_command_spec"> </span> ');
  136. if (expression_arithmetic.first_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  137. renderLogicExpression(command, all_expression, expression_arithmetic.first_operand, function_obj, exp_el_expr_el_1);
  138. } else if (expression_arithmetic.first_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic) {
  139. renderArithmeticExpression(command, all_expression, expression_arithmetic.first_operand, function_obj, exp_el_expr_el_1);
  140. } else {
  141. VariableValueMenuManagement.renderMenu(command, expression_arithmetic.first_operand, exp_el_expr_el_1, function_obj);
  142. }
  143. if (expression_arithmetic.second_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  144. renderLogicExpression(command, all_expression, expression_arithmetic.second_operand, function_obj, exp_el_expr_el_2);
  145. } else if (expression_arithmetic.second_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic) {
  146. renderArithmeticExpression(command, all_expression, expression_arithmetic.second_operand, function_obj, exp_el_expr_el_2);
  147. } else {
  148. VariableValueMenuManagement.renderMenu(command, expression_arithmetic.second_operand, exp_el_expr_el_2, function_obj);
  149. }
  150. renderArithmeticOperator(command, all_expression, expression_arithmetic, expression_arithmetic.operator, function_obj, exp_el_expr_operand);
  151. element_to_append.append(exp_el_par_1);
  152. element_to_append.append(exp_el_expr_el_1);
  153. element_to_append.append(exp_el_expr_operand);
  154. element_to_append.append(exp_el_expr_el_2);
  155. element_to_append.append(exp_el_par_2);
  156. }
  157. function renderStartMenu (command, expression, function_obj, initial_el_to_render) {
  158. var start_menu = '';
  159. start_menu += '<div class="ui dropdown menu_start_rendered"><div class="text"><i>' +
  160. LocalizedStrings.getUI('expression_menu_select') + '</i></div><i class="dropdown icon"></i><div class="menu">';
  161. start_menu += '<div class="item" data-exp="' + Models.EXPRESSION_TYPES.exp_logic + '">' +
  162. LocalizedStrings.getUI('text_logic_expression') + ' (EL == EL and EL)</div>';
  163. start_menu += '<div class="item" data-exp="' + Models.EXPRESSION_TYPES.exp_arithmetic + '">' +
  164. LocalizedStrings.getUI('text_arithmetic_expression') + ' (EA < EA)</div>';
  165. start_menu += '</div></div>';
  166. start_menu = $(start_menu);
  167. start_menu.dropdown({
  168. onChange: function(value, text, $selectedItem) {
  169. switch ($selectedItem.data('exp')) {
  170. case Models.EXPRESSION_TYPES.exp_logic:
  171. expression.expression =
  172. new Models.LogicExpression(false,
  173. new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true));
  174. break;
  175. case Models.EXPRESSION_TYPES.exp_arithmetic:
  176. expression.expression =
  177. new Models.ArithmeticExpression(
  178. new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  179. new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  180. Models.ARITHMETIC_COMPARISON.less_than);
  181. break;
  182. }
  183. initial_el_to_render.html('');
  184. renderExpression(command, expression, function_obj, initial_el_to_render);
  185. },
  186. selectOnKeydown: false
  187. });
  188. initial_el_to_render.append(' <span class="span_command_spec"> </span> ');
  189. initial_el_to_render.append(start_menu);
  190. initial_el_to_render.append(' <span class="span_command_spec"> </span> ');
  191. }