conditional_expression.js 9.7 KB

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