dowhiletrue.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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> <div class="ui context_menu"></div> <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>';
  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')[0]).toggle();
  38. $(el.find('.textual_expression')[0]).text(text);
  39. $(el.find('.textual_expression')[0]).toggle();
  40. $(el.find('.button_alternate_expression')[0]).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('.textual_expression')[0]).toggle();
  51. dowhiletrue_dom.find('.button_alternate_expression').on('click', function() {
  52. if (command.expression) {
  53. var text = CodeGenerator.elementExpressionCode(command.expression);
  54. if (text) {
  55. $(dowhiletrue_dom.find('.conditional_expression')[0]).toggle();
  56. $(dowhiletrue_dom.find('.textual_expression')[0]).text(text);
  57. $(dowhiletrue_dom.find('.textual_expression')[0]).toggle();
  58. $(this).toggleClass('unlock').toggleClass('lock');
  59. command.lockexpression = !command.lockexpression;
  60. }
  61. }
  62. });
  63. dowhiletrue_dom.find('.button_remove_command').on('click', function() {
  64. if (CommandsManagement.removeCommand(command, function_obj, dowhiletrue_dom)) {
  65. dowhiletrue_dom.fadeOut(400, function() {
  66. dowhiletrue_dom.remove();
  67. });
  68. }
  69. });
  70. }