repeatNtimes.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. export function createFloatingCommand () {
  8. return $('<div class="ui repeatNtimes created_element"> <i class="ui icon small sync"></i> <span> para (x = 0; x < 10; x ++) <br> </span></div>');
  9. }
  10. export function renderCommand (command, function_obj) {
  11. var ret = '<div class="ui repeatNtimes command_container"> <i class="ui icon small sync command_drag"></i> <i class="ui icon times red button_remove_command"></i> <div class="ui context_menu"></div> <span class="span_command_spec"> ' + LocalizedStrings.getUI('text_for') + ' ( </span> <div class="ui attribution_expression"><div class="ui variable_attribution"></div> <span class="text_receives span_command_spec"></span> <div class="ui var_value_expression div_expression_st"></div> </div> <span class="span_command_spec separator_character">;</span> <div class="conditional_expression"></div> <span class="span_command_spec separator_character">;</span> <div class="ui incrementation_field"><div class="ui incrementation_variable"></div> <span class="text_inc_receives span_command_spec"></span> <div class="ui first_operand"></div><div class="ui operator"></div><div class="ui second_operand"></div></div> <span class="span_command_spec"> ) </span>';
  12. ret += '<div class="ui block_commands">';
  13. ret += '</div>';
  14. ret += '<span> </span>';
  15. ret += '</div>';
  16. var el = $(ret);
  17. el.data('command', command);
  18. el.find('.block_commands').data('command', command);
  19. addHandlers(command, function_obj, el);
  20. ContextualizedMenu.renderMenu(command, el.find('.context_menu'), function_obj, el);
  21. VariableValueMenu.renderMenu(command, command.var_attribution, el.find('.variable_attribution'), function_obj);
  22. ConditionalExpressionManagement.renderExpression(command, command.expression2, function_obj, el.find('.conditional_expression'));
  23. VariableValueMenu.renderMenu(command, command.var_incrementation, el.find('.incrementation_variable'), function_obj);
  24. if (command.expression1) {
  25. el.find('.text_receives').text(LocalizedStrings.getUI('text_receives'));
  26. VariableValueMenu.renderMenu(command, command.expression1, el.find('.var_value_expression'), function_obj);
  27. }
  28. if (command.expression3) {
  29. el.find('.text_inc_receives').text(LocalizedStrings.getUI('text_receives'));
  30. VariableValueMenu.renderMenu(command, command.expression3.itens[0], el.find('.first_operand'), function_obj);
  31. renderOperator(command, function_obj, el.find('.operator'), command.expression3, 1);
  32. VariableValueMenu.renderMenu(command, command.expression3.itens[2], el.find('.second_operand'), function_obj);
  33. }
  34. if (command.commands_block) {
  35. for (var j = 0; j < command.commands_block.length; j++) {
  36. CommandsManagement.renderCommand(command.commands_block[j], $(el.find('.block_commands')[0]), 3, function_obj);
  37. }
  38. }
  39. return el;
  40. }
  41. export function manageExpressionElements (command, ref_object, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element) {
  42. if (dom_object.hasClass('variable_attribution')) {
  43. if (!command.expression3) {
  44. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp,
  45. [command.var_attribution,
  46. Models.ARITHMETIC_TYPES.plus,
  47. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, "1", null, null, true)]);
  48. command.expression3 = exp;
  49. command.var_incrementation = command.var_attribution;
  50. var cond_exp =
  51. new Models.ArithmeticExpression(
  52. command.var_attribution,
  53. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  54. Models.ARITHMETIC_COMPARISON.less_than);
  55. command.expression2.expression = cond_exp;
  56. }
  57. dom_object.parent().find('.text_receives').text(LocalizedStrings.getUI('text_receives'));
  58. command.expression1 = new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, "0", null, null, true);
  59. dom_object.parent().find('.var_value_expression').empty();
  60. VariableValueMenu.renderMenu(command, command.expression1, dom_object.parent().find('.var_value_expression'), function_obj);
  61. renderAlgorithm();
  62. }
  63. if (dom_object.hasClass('incrementation_variable')) {
  64. dom_object.parent().find('.text_inc_receives').text(LocalizedStrings.getUI('text_receives'));
  65. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp,
  66. [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  67. Models.ARITHMETIC_TYPES.plus,
  68. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  69. command.expression3 = exp;
  70. VariableValueMenu.renderMenu(command, command.expression3.itens[0], dom_object.parent().find('.first_operand'), function_obj);
  71. renderOperator(command, function_obj, dom_object.parent().find('.operator'), command.expression3, 1);
  72. VariableValueMenu.renderMenu(command, command.expression3.itens[2], dom_object.parent().find('.second_operand'), function_obj);
  73. }
  74. }
  75. export function manageClearExpressionElements (command, ref_object, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element) {
  76. if (dom_object.hasClass('variable_attribution')) {
  77. $(dom_object).parent().find('.text_receives').text('');
  78. command.expression1 = null;
  79. $(dom_object).parent().find('.var_value_expression').empty();
  80. }
  81. if (dom_object.hasClass('incrementation_variable')) {
  82. $(dom_object).parent().find('.text_inc_receives').text('');
  83. command.expression3 = null;
  84. $(dom_object).parent().find('.first_operand').empty();
  85. $(dom_object).parent().find('.operator').empty();
  86. $(dom_object).parent().find('.second_operand').empty();
  87. }
  88. }
  89. function addHandlers (command, function_obj, repeatNtimes_dom) {
  90. repeatNtimes_dom.find('.button_remove_command').on('click', function() {
  91. if (CommandsManagement.removeCommand(command, function_obj, repeatNtimes_dom)) {
  92. repeatNtimes_dom.fadeOut(400, function() {
  93. repeatNtimes_dom.remove();
  94. });
  95. }
  96. });
  97. }
  98. function renderOperator (command, function_obj, temp_op, expression_element, index_op) {
  99. var menu_operator = $('<div class="ui dropdown"><div class="text"></div></div>');
  100. menu_operator.dropdown({
  101. values: [
  102. {
  103. name : '+',
  104. value : Models.ARITHMETIC_TYPES.plus,
  105. selected : (expression_element.itens[index_op] == Models.ARITHMETIC_TYPES.plus)
  106. },
  107. {
  108. name : '-',
  109. value : Models.ARITHMETIC_TYPES.minus,
  110. selected : (expression_element.itens[index_op] == Models.ARITHMETIC_TYPES.minus)
  111. },
  112. {
  113. name : '*',
  114. value : Models.ARITHMETIC_TYPES.multiplication,
  115. selected : (expression_element.itens[index_op] == Models.ARITHMETIC_TYPES.multiplication)
  116. },
  117. {
  118. name : '/',
  119. value : Models.ARITHMETIC_TYPES.division,
  120. selected : (expression_element.itens[index_op] == Models.ARITHMETIC_TYPES.division)
  121. },
  122. {
  123. name : '%',
  124. value : Models.ARITHMETIC_TYPES.module,
  125. selected : (expression_element.itens[index_op] == Models.ARITHMETIC_TYPES.module)
  126. }
  127. ],
  128. onChange: function(value, text, $selectedItem) {
  129. expression_element.itens[index_op] = value;
  130. }
  131. });
  132. temp_op.append(menu_operator);
  133. }