repeatNtimes.js 6.1 KB

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