iftrue.js 2.7 KB

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