repeatNtimes.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. import * as CodeGenerator from '../code_generator';
  9. export function createFloatingCommand () {
  10. 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>');
  11. }
  12. export function renderCommand (command, function_obj) {
  13. let ret = `<div class="ui repeatNtimes command_container">
  14. <i class="ui icon small sync command_drag"></i>
  15. <i class="ui icon times red button_remove_command"></i>
  16. <button class="ui icon button minimize_block_button tiny"><i class="icon window minimize"></i></button>
  17. <span class="span_command_spec"> ${LocalizedStrings.getUI('text_for')}
  18. </span> <div class="ui attribution_expression">
  19. <div class="ui variable_attribution"></div>
  20. <span class="text_receives span_command_spec" style="margin-left: 10px;margin-right: 20px;"></span>
  21. <div class="ui var_value_expression div_expression_st"></div>
  22. </div>
  23. <span class="span_command_spec separator_character" style="margin-left: 25px; margin-right: 15px;">
  24. ${LocalizedStrings.getUI("text_for_to")} </span>
  25. <div class="conditional_expression"></div> <div class="pass_button"></div>
  26. <div class="ui incrementation_field"><span class="text_inc_receives span_command_spec"></span>
  27. <div class="ui first_operand"></div><div class="ui operator"></div>
  28. <div class="ui second_operand"></div></div> <span class="span_command_spec"> </span>
  29. <span class="textual_expression"></span> <i class="ui icon unlock button_alternate_expression"></i>
  30. <div class="ui context_menu"></div>`;
  31. ret += '<div class="ui block_commands">';
  32. ret += '</div>';
  33. ret += '<span> </span>';
  34. ret += '</div>';
  35. var el = $(ret);
  36. el.data('command', command);
  37. el.find('.block_commands').data('command', command);
  38. addHandlers(command, function_obj, el);
  39. ContextualizedMenu.renderMenu(command, el.find('.context_menu'), function_obj, el);
  40. VariableValueMenu.renderMenu(command, command.var_attribution, el.find('.variable_attribution'), function_obj);
  41. //ConditionalExpressionManagement.renderExpression(command, command.expression2, function_obj, el.find('.conditional_expression'));
  42. VariableValueMenu.renderMenu(command, command.expression2, el.find('.conditional_expression'), function_obj);
  43. //GenericExpressionManagement.renderExpression(command, function_obj, el.find('.conditional_expression'), command.expression2);
  44. //VariableValueMenu.renderMenu(command, command.var_incrementation, el.find('.incrementation_variable'), function_obj);
  45. //angle double right
  46. if (!command.expression1) {
  47. command.expression1 = new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, "0", null, null, true);
  48. }
  49. el.find('.text_receives').text(LocalizedStrings.getUI("text_for_from"));
  50. VariableValueMenu.renderMenu(command, command.expression1, el.find('.var_value_expression'), function_obj);
  51. if (!command.expression3) {
  52. el.find('.pass_button').html('<button class="ui">' + LocalizedStrings.getUI("text_for_pass") + '<i class="ui icon angle double right"></i></button>');
  53. el.find('.pass_button').find('button').on('click', function() {
  54. togglePasso(command);
  55. });
  56. /*var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp,
  57. [command.var_attribution,
  58. Models.ARITHMETIC_TYPES.plus,
  59. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, "1", null, null, true)]);
  60. command.expression3 = exp;
  61. command.var_incrementation = command.var_attribution;*/
  62. } else {
  63. el.find('.pass_button').html('<button class="ui">' + LocalizedStrings.getUI("text_for_pass") + '<i class="ui icon angle double left"></i></button>');
  64. el.find('.pass_button').find('button').on('click', function() {
  65. togglePasso(command);
  66. });
  67. renderOperator(command, function_obj, el.find('.operator'), command.expression3, 1);
  68. VariableValueMenu.renderMenu(command, command.expression3.itens[2], el.find('.second_operand'), function_obj);
  69. }
  70. if (command.commands_block) {
  71. for (var j = 0; j < command.commands_block.length; j++) {
  72. CommandsManagement.renderCommand(command.commands_block[j], $(el.find('.block_commands')[0]), 3, function_obj);
  73. }
  74. }
  75. if (command.lockexpression) {
  76. try {
  77. var text = CodeGenerator.repeatNtimesHeader(command);
  78. if (text) {
  79. $(el.find('.textual_expression')[0]).html(text);
  80. $(el.find('.textual_expression')[0]).toggle();
  81. $(el.find('.attribution_expression')[0]).toggle();
  82. $(el.find('.span_command_spec.separator_character')[0]).toggle();
  83. $(el.find('.conditional_expression')[0]).toggle();
  84. $(el.find('.pass_button')[0]).toggle();
  85. $(el.find('.incrementation_field')[0]).toggle();
  86. $(el.find('.button_alternate_expression')[0]).toggleClass('unlock').toggleClass('lock');
  87. }
  88. } catch (e) {
  89. command.lockexpression = false;
  90. }
  91. }
  92. return el;
  93. }
  94. function togglePasso (command) {
  95. if (!command.expression3) {
  96. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp,
  97. [command.var_attribution,
  98. Models.ARITHMETIC_TYPES.plus,
  99. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, "1", null, null, true)]);
  100. command.expression3 = exp;
  101. command.var_incrementation = command.var_attribution;
  102. } else {
  103. command.expression3 = null;
  104. }
  105. renderAlgorithm();
  106. }
  107. export function manageExpressionElements (command, ref_object, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element) {
  108. if (dom_object.hasClass('variable_attribution')) {
  109. if (!command.expression3) {
  110. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp,
  111. [command.var_attribution,
  112. Models.ARITHMETIC_TYPES.plus,
  113. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, "1", null, null, true)]);
  114. command.expression3 = exp;
  115. command.var_incrementation = command.var_attribution;
  116. var cond_exp =
  117. new Models.ArithmeticExpression(
  118. command.var_attribution,
  119. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  120. Models.ARITHMETIC_COMPARISON.less_than);
  121. command.expression2.expression = cond_exp;
  122. }
  123. dom_object.parent().find('.text_receives').text('de');
  124. command.expression1 = new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, "0", null, null, true);
  125. dom_object.parent().find('.var_value_expression').empty();
  126. VariableValueMenu.renderMenu(command, command.expression1, dom_object.parent().find('.var_value_expression'), function_obj);
  127. renderAlgorithm();
  128. }
  129. if (dom_object.hasClass('incrementation_variable')) {
  130. dom_object.parent().find('.text_inc_receives').text('de');
  131. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp,
  132. [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  133. Models.ARITHMETIC_TYPES.plus,
  134. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  135. command.expression3 = exp;
  136. VariableValueMenu.renderMenu(command, command.expression3.itens[0], dom_object.parent().find('.first_operand'), function_obj);
  137. renderOperator(command, function_obj, dom_object.parent().find('.operator'), command.expression3, 1);
  138. VariableValueMenu.renderMenu(command, command.expression3.itens[2], dom_object.parent().find('.second_operand'), function_obj);
  139. }
  140. }
  141. export function manageClearExpressionElements (command, ref_object, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element) {
  142. if (dom_object.hasClass('variable_attribution')) {
  143. $(dom_object).parent().find('.text_receives').text('');
  144. command.expression1 = null;
  145. $(dom_object).parent().find('.var_value_expression').empty();
  146. }
  147. if (dom_object.hasClass('incrementation_variable')) {
  148. $(dom_object).parent().find('.text_inc_receives').text('');
  149. command.expression3 = null;
  150. $(dom_object).parent().find('.first_operand').empty();
  151. $(dom_object).parent().find('.operator').empty();
  152. $(dom_object).parent().find('.second_operand').empty();
  153. }
  154. }
  155. function addHandlers (command, function_obj, repeatNtimes_dom) {
  156. repeatNtimes_dom.find('.minimize_block_button').on('click', function(evt){
  157. repeatNtimes_dom.children('.ui.block_commands').toggle();
  158. command.collapsed = !command.collapsed;
  159. });
  160. $(repeatNtimes_dom.find('.textual_expression')[0]).toggle();
  161. repeatNtimes_dom.find('.button_alternate_expression').on('click', function() {
  162. var text = CodeGenerator.repeatNtimesHeader(command);
  163. if (text) {
  164. $(repeatNtimes_dom.find('.textual_expression')[0]).html(text);
  165. $(repeatNtimes_dom.find('.textual_expression')[0]).toggle();
  166. $(repeatNtimes_dom.find('.attribution_expression')[0]).toggle();
  167. $(repeatNtimes_dom.find('.span_command_spec.separator_character')[0]).toggle();
  168. $(repeatNtimes_dom.find('.conditional_expression')[0]).toggle();
  169. $(repeatNtimes_dom.find('.pass_button')[0]).toggle();
  170. $(repeatNtimes_dom.find('.incrementation_field')[0]).toggle();
  171. $(repeatNtimes_dom.find('.button_alternate_expression')[0]).toggleClass('unlock').toggleClass('lock');
  172. command.lockexpression = !command.lockexpression;
  173. }
  174. });
  175. repeatNtimes_dom.find('.button_remove_command').on('click', function() {
  176. if (CommandsManagement.removeCommand(command, function_obj, repeatNtimes_dom)) {
  177. repeatNtimes_dom.fadeOut(400, function() {
  178. repeatNtimes_dom.remove();
  179. });
  180. }
  181. });
  182. }
  183. function renderOperator (command, function_obj, temp_op, expression_element, index_op) {
  184. var menu_operator = $('<div class="ui dropdown"><div class="text"></div></div>');
  185. menu_operator.dropdown({
  186. values: [
  187. {
  188. name : '+',
  189. value : Models.ARITHMETIC_TYPES.plus,
  190. selected : (expression_element.itens[index_op] == Models.ARITHMETIC_TYPES.plus)
  191. },
  192. {
  193. name : '-',
  194. value : Models.ARITHMETIC_TYPES.minus,
  195. selected : (expression_element.itens[index_op] == Models.ARITHMETIC_TYPES.minus)
  196. }
  197. ],
  198. onChange: function(value, text, $selectedItem) {
  199. expression_element.itens[index_op] = value;
  200. }
  201. });
  202. temp_op.append(menu_operator);
  203. }