repeatNtimes.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import * as Models from '../ivprog_elements';
  2. import { LocalizedStrings } from '../../services/localizedStringsService';
  3. import * as CommandsManagement from '../commands';
  4. import * as ConditionalExpressionManagement from './conditional_expression';
  5. import * as VariableValueMenu from './variable_value_menu';
  6. import * as ContextualizedMenu from './contextualized_menu';
  7. import * as GenericExpressionManagement from './generic_expression';
  8. export function createFloatingCommand () {
  9. return $('<div class="ui repeatNtimes created_element"> <i class="ui icon small sync"></i> <span> repita_para i de 0 ate 10 passo 1 <br> </span></div>');
  10. }
  11. export function renderCommand (command, function_obj) {
  12. let ret = `<div class="ui repeatNtimes command_container">
  13. <i class="ui icon small sync command_drag"></i>
  14. <i class="ui icon times red button_remove_command"></i>
  15. <div class="ui context_menu"></div> <span class="span_command_spec"> ${LocalizedStrings.getUI('text_for')}
  16. </span> <div class="ui attribution_expression">
  17. <div class="ui variable_attribution"></div>
  18. <span class="text_receives span_command_spec" style="margin-left: 10px;margin-right: 20px;"></span>
  19. <div class="ui var_value_expression div_expression_st"></div>
  20. </div>
  21. <span class="span_command_spec separator_character" style="margin-left: 25px; margin-right: 15px;">
  22. ${LocalizedStrings.getUI("text_for_to")} </span>
  23. <div class="conditional_expression"></div> <div class="pass_button"></div>
  24. <div class="ui incrementation_field"><span class="text_inc_receives span_command_spec"></span>
  25. <div class="ui first_operand"></div><div class="ui operator"></div>
  26. <div class="ui second_operand"></div></div> <span class="span_command_spec"> </span>'`;
  27. ret += '<div class="ui block_commands">';
  28. ret += '</div>';
  29. ret += '<span> </span>';
  30. ret += '</div>';
  31. var el = $(ret);
  32. el.data('command', command);
  33. el.find('.block_commands').data('command', command);
  34. addHandlers(command, function_obj, el);
  35. ContextualizedMenu.renderMenu(command, el.find('.context_menu'), function_obj, el);
  36. VariableValueMenu.renderMenu(command, command.var_attribution, el.find('.variable_attribution'), function_obj);
  37. //ConditionalExpressionManagement.renderExpression(command, command.expression2, function_obj, el.find('.conditional_expression'));
  38. VariableValueMenu.renderMenu(command, command.expression2, el.find('.conditional_expression'), function_obj);
  39. //GenericExpressionManagement.renderExpression(command, function_obj, el.find('.conditional_expression'), command.expression2);
  40. //VariableValueMenu.renderMenu(command, command.var_incrementation, el.find('.incrementation_variable'), function_obj);
  41. //angle double right
  42. if (!command.expression1) {
  43. command.expression1 = new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, "0", null, null, true);
  44. }
  45. el.find('.text_receives').text(LocalizedStrings.getUI("text_for_from"));
  46. VariableValueMenu.renderMenu(command, command.expression1, el.find('.var_value_expression'), function_obj);
  47. if (!command.expression3) {
  48. el.find('.pass_button').html('<button class="ui">' + LocalizedStrings.getUI("text_for_pass") + '<i class="ui icon angle double right"></i></button>');
  49. el.find('.pass_button').find('button').on('click', function() {
  50. togglePasso(command);
  51. });
  52. /*var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp,
  53. [command.var_attribution,
  54. Models.ARITHMETIC_TYPES.plus,
  55. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, "1", null, null, true)]);
  56. command.expression3 = exp;
  57. command.var_incrementation = command.var_attribution;*/
  58. } else {
  59. el.find('.pass_button').html('<button class="ui">' + LocalizedStrings.getUI("text_for_pass") + '<i class="ui icon angle double left"></i></button>');
  60. el.find('.pass_button').find('button').on('click', function() {
  61. togglePasso(command);
  62. });
  63. renderOperator(command, function_obj, el.find('.operator'), command.expression3, 1);
  64. VariableValueMenu.renderMenu(command, command.expression3.itens[2], el.find('.second_operand'), function_obj);
  65. }
  66. if (command.commands_block) {
  67. for (var j = 0; j < command.commands_block.length; j++) {
  68. CommandsManagement.renderCommand(command.commands_block[j], $(el.find('.block_commands')[0]), 3, function_obj);
  69. }
  70. }
  71. return el;
  72. }
  73. function togglePasso (command) {
  74. if (!command.expression3) {
  75. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp,
  76. [command.var_attribution,
  77. Models.ARITHMETIC_TYPES.plus,
  78. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, "1", null, null, true)]);
  79. command.expression3 = exp;
  80. command.var_incrementation = command.var_attribution;
  81. } else {
  82. command.expression3 = null;
  83. }
  84. renderAlgorithm();
  85. }
  86. export function manageExpressionElements (command, ref_object, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element) {
  87. if (dom_object.hasClass('variable_attribution')) {
  88. if (!command.expression3) {
  89. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp,
  90. [command.var_attribution,
  91. Models.ARITHMETIC_TYPES.plus,
  92. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, "1", null, null, true)]);
  93. command.expression3 = exp;
  94. command.var_incrementation = command.var_attribution;
  95. var cond_exp =
  96. new Models.ArithmeticExpression(
  97. command.var_attribution,
  98. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  99. Models.ARITHMETIC_COMPARISON.less_than);
  100. command.expression2.expression = cond_exp;
  101. }
  102. dom_object.parent().find('.text_receives').text('de');
  103. command.expression1 = new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, "0", null, null, true);
  104. dom_object.parent().find('.var_value_expression').empty();
  105. VariableValueMenu.renderMenu(command, command.expression1, dom_object.parent().find('.var_value_expression'), function_obj);
  106. renderAlgorithm();
  107. }
  108. if (dom_object.hasClass('incrementation_variable')) {
  109. dom_object.parent().find('.text_inc_receives').text('de');
  110. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp,
  111. [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  112. Models.ARITHMETIC_TYPES.plus,
  113. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  114. command.expression3 = exp;
  115. VariableValueMenu.renderMenu(command, command.expression3.itens[0], dom_object.parent().find('.first_operand'), function_obj);
  116. renderOperator(command, function_obj, dom_object.parent().find('.operator'), command.expression3, 1);
  117. VariableValueMenu.renderMenu(command, command.expression3.itens[2], dom_object.parent().find('.second_operand'), function_obj);
  118. }
  119. }
  120. export function manageClearExpressionElements (command, ref_object, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element) {
  121. if (dom_object.hasClass('variable_attribution')) {
  122. $(dom_object).parent().find('.text_receives').text('');
  123. command.expression1 = null;
  124. $(dom_object).parent().find('.var_value_expression').empty();
  125. }
  126. if (dom_object.hasClass('incrementation_variable')) {
  127. $(dom_object).parent().find('.text_inc_receives').text('');
  128. command.expression3 = null;
  129. $(dom_object).parent().find('.first_operand').empty();
  130. $(dom_object).parent().find('.operator').empty();
  131. $(dom_object).parent().find('.second_operand').empty();
  132. }
  133. }
  134. function addHandlers (command, function_obj, repeatNtimes_dom) {
  135. repeatNtimes_dom.find('.button_remove_command').on('click', function() {
  136. if (CommandsManagement.removeCommand(command, function_obj, repeatNtimes_dom)) {
  137. repeatNtimes_dom.fadeOut(400, function() {
  138. repeatNtimes_dom.remove();
  139. });
  140. }
  141. });
  142. }
  143. function renderOperator (command, function_obj, temp_op, expression_element, index_op) {
  144. var menu_operator = $('<div class="ui dropdown"><div class="text"></div></div>');
  145. menu_operator.dropdown({
  146. values: [
  147. {
  148. name : '+',
  149. value : Models.ARITHMETIC_TYPES.plus,
  150. selected : (expression_element.itens[index_op] == Models.ARITHMETIC_TYPES.plus)
  151. },
  152. {
  153. name : '-',
  154. value : Models.ARITHMETIC_TYPES.minus,
  155. selected : (expression_element.itens[index_op] == Models.ARITHMETIC_TYPES.minus)
  156. }
  157. ],
  158. onChange: function(value, text, $selectedItem) {
  159. expression_element.itens[index_op] = value;
  160. }
  161. });
  162. temp_op.append(menu_operator);
  163. }