iftrue.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { LocalizedStrings } from '../../services/localizedStringsService';
  2. import * as CommandsManagement from '../commands';
  3. import * as ConditionalExpressionManagement from './conditional_expression';
  4. export function createFloatingCommand () {
  5. return $('<div class="ui iftrue created_element"> <i class="ui icon small random"></i> <span> if (x < 1) { } </span></div>');
  6. }
  7. export function renderCommand (command, function_obj) {
  8. var ret = '';
  9. ret += '<div class="ui iftrue command_container"><div class="ui data_block_if" data-if="true"> <i class="ui icon small random command_drag"></i> <i class="ui icon times red button_remove_command"></i>';
  10. ret += '<span class="span_command_spec"> ' + LocalizedStrings.getUI('text_if') + '</span>';
  11. ret += ' <span class="span_command_spec"> ( </span> <div class="conditional_expression"></div> <span class="span_command_spec"> ) </span>';
  12. ret += '<span> </span> ';
  13. ret += '<div class="ui block_commands commands_if conditional_comands_block" data-if="true">';
  14. ret += '</div></div>';
  15. ret += '<div class="ui data_block_else" data-else="true"> <span class="span_command_spec"> ' + LocalizedStrings.getUI('text_else') + ' </span>';
  16. ret += '<div class="ui block_commands commands_else conditional_comands_block" data-else="true">';
  17. ret += '</div>';
  18. ret += '<span></span></div>';
  19. ret += '</div>';
  20. var el = $(ret);
  21. el.data('command', command);
  22. el.find('.block_commands').data('command', command);
  23. el.find('.data_block_if').data('command', command);
  24. el.find('.data_block_else').data('command', command);
  25. el.find('.commands_if').data('command', command);
  26. addHandlers(command, function_obj, el);
  27. ConditionalExpressionManagement.renderExpression(command, command.expression, function_obj, el.find('.conditional_expression'));
  28. if (command.commands_block) {
  29. for (var j = 0; j < command.commands_block.length; j++) {
  30. CommandsManagement.renderCommand(command.commands_block[j], $(el.find('.commands_if')[0]), 3, function_obj);
  31. }
  32. }
  33. if (command.commands_else) {
  34. for (var j = 0; j < command.commands_else.length; j++) {
  35. CommandsManagement.renderCommand(command.commands_else[j], $(el.find('.commands_else')[0]), 3, function_obj);
  36. }
  37. }
  38. return el;
  39. }
  40. function addHandlers (command, function_obj, iftrue_dom) {
  41. iftrue_dom.find('.button_remove_command').on('click', function() {
  42. if (CommandsManagement.removeCommand(command, function_obj, iftrue_dom)) {
  43. iftrue_dom.fadeOut(400, function() {
  44. iftrue_dom.remove();
  45. });
  46. }
  47. });
  48. }