1
0

conditional_expression.js 10 KB

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