repeatNtimes.js 7.8 KB

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