dowhiletrue.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { LocalizedStrings } from '../../services/localizedStringsService';
  2. import * as CommandsManagement from '../commands';
  3. import * as ConditionalExpressionManagement from './conditional_expression';
  4. import * as ContextualizedMenu from './contextualized_menu';
  5. import * as GenericExpressionManagement from './generic_expression';
  6. import * as CodeGenerator from '../code_generator';
  7. export function createFloatingCommand () {
  8. return $('<div class="ui dowhiletrue created_element"> <i class="ui icon small sync"></i> <span> '+ LocalizedStrings.getUI('text_command_do') +' <br> ' + LocalizedStrings.getUI('text_command_do_until') +'(x < 10) </span></div>');
  9. }
  10. export function renderCommand (command, function_obj) {
  11. var ret = '';
  12. ret += '<div class="ui dowhiletrue command_container"> <i class="ui icon small sync command_drag"></i> <i class="ui icon times red button_remove_command"></i> <button class="ui icon button minimize_block_button tiny"><i class="icon window minimize"></i></button> <span class="span_command_spec"> ' + LocalizedStrings.getUI('text_command_do') + ' </span>';
  13. ret += '<div class="ui block_commands" data-subblock="" data-idcommand="">';
  14. ret += '</div>';
  15. ret += ' <span class="span_command_spec"> ' + LocalizedStrings.getUI('text_command_do_until') + ' </span> <span class="span_command_spec"> ( </span> <div class="conditional_expression"></div> <span class="textual_expression"></span> <span class="span_command_spec"> ) </span>';
  16. ret += ' <i class="ui icon unlock button_alternate_expression"></i> <div class="ui context_menu"></div> ';
  17. ret += '</div>';
  18. var el = $(ret);
  19. el.data('command', command);
  20. el.find('.block_commands').data('command', command);
  21. addHandlers(command, function_obj, el);
  22. ContextualizedMenu.renderMenu(command, el.find('.context_menu'), function_obj, el);
  23. //ConditionalExpressionManagement.renderExpression(command, command.expression, function_obj, el.find('.conditional_expression'));
  24. if (command.expression) {
  25. GenericExpressionManagement.renderExpression(command, function_obj, el.find('.conditional_expression'), command.expression);
  26. }
  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. if (command.lockexpression) {
  33. if (command.expression) {
  34. try {
  35. var text = CodeGenerator.elementExpressionCode(command.expression);
  36. if (text) {
  37. $(el.find('.conditional_expression')[el.find('.conditional_expression').length - 1]).toggle();
  38. $(el.find('.textual_expression')[el.find('.textual_expression').length - 1]).text(text);
  39. $(el.find('.textual_expression')[el.find('.textual_expression').length - 1]).toggle();
  40. $(el.find('.button_alternate_expression')[el.find('.button_alternate_expression').length - 1]).toggleClass('unlock').toggleClass('lock');
  41. }
  42. } catch (e) {
  43. command.lockexpression = false;
  44. }
  45. }
  46. }
  47. return el;
  48. }
  49. function addHandlers (command, function_obj, dowhiletrue_dom) {
  50. dowhiletrue_dom.find('.minimize_block_button').on('click', function(evt){
  51. dowhiletrue_dom.children('.ui.block_commands').toggle();
  52. command.collapsed = !command.collapsed;
  53. });
  54. $(dowhiletrue_dom.find('.textual_expression')[0]).toggle();
  55. dowhiletrue_dom.find('.button_alternate_expression').on('click', function() {
  56. if (command.expression) {
  57. var text = CodeGenerator.elementExpressionCode(command.expression);
  58. if (text) {
  59. $(dowhiletrue_dom.find('.conditional_expression')[dowhiletrue_dom.find('.conditional_expression').length - 1]).toggle();
  60. $(dowhiletrue_dom.find('.textual_expression')[dowhiletrue_dom.find('.textual_expression').length - 1]).text(text);
  61. $(dowhiletrue_dom.find('.textual_expression')[dowhiletrue_dom.find('.textual_expression').length - 1]).toggle();
  62. $(this).toggleClass('unlock').toggleClass('lock');
  63. command.lockexpression = !command.lockexpression;
  64. }
  65. }
  66. });
  67. dowhiletrue_dom.find('.button_remove_command').on('click', function() {
  68. if (CommandsManagement.removeCommand(command, function_obj, dowhiletrue_dom)) {
  69. dowhiletrue_dom.fadeOut(400, function() {
  70. dowhiletrue_dom.remove();
  71. });
  72. }
  73. });
  74. }