whiletrue.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 whiletrue created_element"> <i class="ui icon small sync"></i> <span> ' + LocalizedStrings.getUI('text_command_while') + ' ( x < 10 ) <br> </span></div>');
  9. }
  10. export function renderCommand (command, function_obj) {
  11. var ret = '';
  12. ret += '<div class="ui whiletrue 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_while') + ' </span>';
  13. ret += '<span class="span_command_spec"> ( </span> <div class="conditional_expression"></div> <span class="textual_expression"></span> <span class="span_command_spec"> ) </span>';
  14. ret += ' </span>';
  15. ret += '<i class="ui icon unlock button_alternate_expression"></i> <div class="ui context_menu"></div>';
  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. el.find('.block_commands').data('command', command);
  23. addHandlers(command, function_obj, el);
  24. ContextualizedMenu.renderMenu(command, el.find('.context_menu'), function_obj, el);
  25. //ConditionalExpressionManagement.renderExpression(command, command.expression, function_obj, el.find('.conditional_expression'));
  26. GenericExpressionManagement.renderExpression(command, function_obj, el.find('.conditional_expression'), command.expression);
  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, whiletrue_dom) {
  50. whiletrue_dom.find('.minimize_block_button').on('click', function(evt){
  51. whiletrue_dom.children('.ui.block_commands').toggle();
  52. command.collapsed = !command.collapsed;
  53. });
  54. $(whiletrue_dom.find('.textual_expression')[0]).toggle();
  55. whiletrue_dom.find('.button_alternate_expression').on('click', function() {
  56. if (command.expression) {
  57. var text = CodeGenerator.elementExpressionCode(command.expression);
  58. if (text) {
  59. $(whiletrue_dom.find('.conditional_expression')[0]).toggle();
  60. $(whiletrue_dom.find('.textual_expression')[0]).text(text);
  61. $(whiletrue_dom.find('.textual_expression')[0]).toggle();
  62. $(this).toggleClass('unlock').toggleClass('lock');
  63. command.lockexpression = !command.lockexpression;
  64. }
  65. }
  66. });
  67. whiletrue_dom.find('.button_remove_command').on('click', function() {
  68. if (CommandsManagement.removeCommand(command, function_obj, whiletrue_dom)) {
  69. whiletrue_dom.fadeOut(400, function() {
  70. whiletrue_dom.remove();
  71. });
  72. }
  73. });
  74. }