conditional_expression.js 9.8 KB

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