iftrue.js 2.8 KB

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