1
0

conditional_expression.js 10 KB

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