iftrue.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { LocalizedStrings } from '../../services/localizedStringsService';
  2. import * as CommandsManagement from '../commands';
  3. import * as GenericExpressionManagement from './generic_expression';
  4. import * as CodeGenerator from '../code_generator';
  5. export function createFloatingCommand () {
  6. return $('<div class="ui iftrue created_element"> <i class="ui icon small random"></i> <span> if (x < 1) { } </span></div>');
  7. }
  8. export function renderCommand (command, function_obj) {
  9. var ret = '';
  10. 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> <button class="ui icon button minimize_block_button tiny"><i class="icon window minimize"></i></button>';
  11. ret += '<span class="span_command_spec"> ' + LocalizedStrings.getUI('text_if') + '</span>';
  12. ret += ' <span class="span_command_spec"> ( </span> <div class="conditional_expression"></div> <span class="textual_expression"></span> <span class="span_command_spec"> ) </span> ';
  13. ret += '<i class="ui icon unlock button_alternate_expression"></i>';
  14. ret += '<span> </span> ';
  15. ret += '<div class="ui block_commands commands_if conditional_comands_block" data-if="true">';
  16. ret += '</div></div>';
  17. ret += '<div class="ui data_block_else" data-else="true"> <span class="span_command_spec"> ' + LocalizedStrings.getUI('text_else') + ' </span>';
  18. ret += '<div class="ui block_commands commands_else conditional_comands_block" data-else="true">';
  19. ret += '</div>';
  20. ret += '<span></span></div>';
  21. ret += '</div>';
  22. var el = $(ret);
  23. el.data('command', command);
  24. el.find('.block_commands').data('command', command);
  25. el.find('.data_block_if').data('command', command);
  26. el.find('.data_block_else').data('command', command);
  27. el.find('.commands_if').data('command', command);
  28. addHandlers(command, function_obj, el);
  29. //ConditionalExpressionManagement.renderExpression(command, command.expression, function_obj, el.find('.conditional_expression'));
  30. GenericExpressionManagement.renderExpression(command, function_obj, el.find('.conditional_expression'), command.expression);
  31. if (command.commands_block) {
  32. for (var j = 0; j < command.commands_block.length; j++) {
  33. CommandsManagement.renderCommand(command.commands_block[j], $(el.find('.commands_if')[0]), 3, function_obj);
  34. }
  35. }
  36. if (command.commands_else) {
  37. for (var j = 0; j < command.commands_else.length; j++) {
  38. CommandsManagement.renderCommand(command.commands_else[j], $(el.find('.commands_else')[0]), 3, function_obj);
  39. }
  40. }
  41. if (command.collapsed) {
  42. el.children('.data_block_else').toggle();
  43. $(el.find('.block_commands')[0]).toggle();
  44. }
  45. if (command.lockexpression) {
  46. if (command.expression) {
  47. try {
  48. var text = CodeGenerator.elementExpressionCode(command.expression);
  49. if (text) {
  50. $(el.find('.conditional_expression')[0]).toggle();
  51. $(el.find('.textual_expression')[0]).text(text);
  52. $(el.find('.textual_expression')[0]).toggle();
  53. $(el.find('.button_alternate_expression')[0]).toggleClass('unlock').toggleClass('lock');
  54. }
  55. } catch (e) {
  56. command.lockexpression = false;
  57. }
  58. }
  59. }
  60. return el;
  61. }
  62. function addHandlers (command, function_obj, iftrue_dom) {
  63. $(iftrue_dom.find('.textual_expression')[0]).toggle();
  64. iftrue_dom.find('.button_alternate_expression').on('click', function() {
  65. if (command.expression) {
  66. var text = CodeGenerator.elementExpressionCode(command.expression);
  67. if (text) {
  68. $(iftrue_dom.find('.conditional_expression')[0]).toggle();
  69. $(iftrue_dom.find('.textual_expression')[0]).text(text);
  70. $(iftrue_dom.find('.textual_expression')[0]).toggle();
  71. $(this).toggleClass('unlock').toggleClass('lock');
  72. command.lockexpression = !command.lockexpression;
  73. }
  74. }
  75. });
  76. iftrue_dom.find('.button_remove_command').on('click', function() {
  77. if (CommandsManagement.removeCommand(command, function_obj, iftrue_dom)) {
  78. iftrue_dom.fadeOut(400, function() {
  79. iftrue_dom.remove();
  80. });
  81. }
  82. });
  83. iftrue_dom.find('.minimize_block_button').on('click', function() {
  84. iftrue_dom.children('.data_block_else').toggle();
  85. $(iftrue_dom.find('.block_commands')[0]).toggle();
  86. command.collapsed = !command.collapsed;
  87. });
  88. }