repeatNtimes.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. el.find('.unlock').popup({
  93. content : LocalizedStrings.getUI("text_lock_expression"),
  94. delay: {
  95. show: 750,
  96. hide: 0
  97. }
  98. });
  99. el.find('.lock').popup({
  100. content : LocalizedStrings.getUI("text_unlock_expression"),
  101. delay: {
  102. show: 750,
  103. hide: 0
  104. }
  105. });
  106. return el;
  107. }
  108. function togglePasso (command) {
  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. } else {
  117. command.expression3 = null;
  118. }
  119. renderAlgorithm();
  120. }
  121. export function manageExpressionElements (command, ref_object, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element) {
  122. if (dom_object.hasClass('variable_attribution')) {
  123. if (!command.expression3) {
  124. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp,
  125. [command.var_attribution,
  126. Models.ARITHMETIC_TYPES.plus,
  127. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, "1", null, null, true)]);
  128. command.expression3 = exp;
  129. command.var_incrementation = command.var_attribution;
  130. var cond_exp =
  131. new Models.ArithmeticExpression(
  132. command.var_attribution,
  133. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  134. Models.ARITHMETIC_COMPARISON.less_than);
  135. command.expression2.expression = cond_exp;
  136. }
  137. dom_object.parent().find('.text_receives').text('de');
  138. command.expression1 = new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, "0", null, null, true);
  139. dom_object.parent().find('.var_value_expression').empty();
  140. VariableValueMenu.renderMenu(command, command.expression1, dom_object.parent().find('.var_value_expression'), function_obj);
  141. renderAlgorithm();
  142. }
  143. if (dom_object.hasClass('incrementation_variable')) {
  144. dom_object.parent().find('.text_inc_receives').text('de');
  145. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp,
  146. [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  147. Models.ARITHMETIC_TYPES.plus,
  148. new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  149. command.expression3 = exp;
  150. VariableValueMenu.renderMenu(command, command.expression3.itens[0], dom_object.parent().find('.first_operand'), function_obj);
  151. renderOperator(command, function_obj, dom_object.parent().find('.operator'), command.expression3, 1);
  152. VariableValueMenu.renderMenu(command, command.expression3.itens[2], dom_object.parent().find('.second_operand'), function_obj);
  153. }
  154. }
  155. export function manageClearExpressionElements (command, ref_object, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element) {
  156. if (dom_object.hasClass('variable_attribution')) {
  157. $(dom_object).parent().find('.text_receives').text('');
  158. command.expression1 = null;
  159. $(dom_object).parent().find('.var_value_expression').empty();
  160. }
  161. if (dom_object.hasClass('incrementation_variable')) {
  162. $(dom_object).parent().find('.text_inc_receives').text('');
  163. command.expression3 = null;
  164. $(dom_object).parent().find('.first_operand').empty();
  165. $(dom_object).parent().find('.operator').empty();
  166. $(dom_object).parent().find('.second_operand').empty();
  167. }
  168. }
  169. function addHandlers (command, function_obj, repeatNtimes_dom) {
  170. repeatNtimes_dom.find('.minimize_block_button').on('click', function(evt){
  171. repeatNtimes_dom.children('.ui.block_commands').toggle();
  172. command.collapsed = !command.collapsed;
  173. });
  174. $(repeatNtimes_dom.find('.textual_expression')[0]).toggle();
  175. repeatNtimes_dom.find('.button_alternate_expression').on('click', function() {
  176. var text = CodeGenerator.repeatNtimesHeader(command);
  177. if (text) {
  178. $(repeatNtimes_dom.find('.textual_expression')[0]).html(text);
  179. $(repeatNtimes_dom.find('.textual_expression')[0]).toggle();
  180. $(repeatNtimes_dom.find('.attribution_expression')[0]).toggle();
  181. $(repeatNtimes_dom.find('.span_command_spec.separator_character')[0]).toggle();
  182. $(repeatNtimes_dom.find('.conditional_expression')[0]).toggle();
  183. $(repeatNtimes_dom.find('.pass_button')[0]).toggle();
  184. $(repeatNtimes_dom.find('.incrementation_field')[0]).toggle();
  185. $(repeatNtimes_dom.find('.button_alternate_expression')[0]).toggleClass('unlock').toggleClass('lock');
  186. command.lockexpression = !command.lockexpression;
  187. }
  188. if (command.lockexpression) {
  189. repeatNtimes_dom.find('.lock').popup({
  190. content : LocalizedStrings.getUI("text_unlock_expression"),
  191. delay: {
  192. show: 750,
  193. hide: 0
  194. }
  195. });
  196. } else {
  197. repeatNtimes_dom.find('.unlock').popup({
  198. content : LocalizedStrings.getUI("text_lock_expression"),
  199. delay: {
  200. show: 750,
  201. hide: 0
  202. }
  203. });
  204. }
  205. });
  206. repeatNtimes_dom.find('.button_remove_command').on('click', function() {
  207. if (CommandsManagement.removeCommand(command, function_obj, repeatNtimes_dom)) {
  208. repeatNtimes_dom.fadeOut(400, function() {
  209. repeatNtimes_dom.remove();
  210. });
  211. }
  212. });
  213. }
  214. function renderOperator (command, function_obj, temp_op, expression_element, index_op) {
  215. var menu_operator = $('<div class="ui dropdown"><div class="text"></div></div>');
  216. menu_operator.dropdown({
  217. values: [
  218. {
  219. name : '+',
  220. value : Models.ARITHMETIC_TYPES.plus,
  221. selected : (expression_element.itens[index_op] == Models.ARITHMETIC_TYPES.plus)
  222. },
  223. {
  224. name : '-',
  225. value : Models.ARITHMETIC_TYPES.minus,
  226. selected : (expression_element.itens[index_op] == Models.ARITHMETIC_TYPES.minus)
  227. }
  228. ],
  229. onChange: function(value, text, $selectedItem) {
  230. expression_element.itens[index_op] = value;
  231. }
  232. });
  233. temp_op.append(menu_operator);
  234. }