return.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { Types } from '../types';
  2. import { LocalizedStrings } from '../../services/localizedStringsService';
  3. import * as VariableValueMenu from './variable_value_menu';
  4. import * as CommandsManagement from '../commands';
  5. import * as GenericExpressionManagement from './generic_expression';
  6. import * as Models from '../ivprog_elements';
  7. import * as CodeGenerator from '../code_generator';
  8. export function createFloatingCommand () {
  9. return $('<div class="ui return created_element"> <i class="ui icon small reply"></i> <span> '+LocalizedStrings.getUI('text_return')+' </span></div>');
  10. }
  11. export function renderCommand (command, function_obj) {
  12. var el = $('<div class="ui return command_container"> <i class="ui icon small reply command_drag"></i> <i class="ui icon times red button_remove_command"></i> <span class="span_command_spec"> '+LocalizedStrings.getUI('text_return')+' </span> <div class="expression_elements"></div><span class="textual_expression" style="display: none; margin-left: 1rem;"></span></div>');
  13. el.data('command', command);
  14. if (function_obj.return_type != Types.VOID) {
  15. if (!command.variable_value_menu) {
  16. command.variable_value_menu = [new Models.VariableValueMenu(VariableValueMenu.VAR_OR_VALUE_TYPES.all, null, null, null, true)];
  17. }
  18. GenericExpressionManagement.renderExpression(command, function_obj, el.find('.expression_elements'), command.variable_value_menu);
  19. el.append($('<i class="ui icon unlock button_alternate_expression" style="margin-left: 1rem;"></i>'));
  20. if (command.lockexpression) {
  21. if (command.variable_value_menu) {
  22. try {
  23. var text = CodeGenerator.elementExpressionCode(command.variable_value_menu);
  24. if (text) {
  25. $(el.find('.expression_elements')[0]).toggle();
  26. $(el.find('.textual_expression')[0]).text(text);
  27. $(el.find('.textual_expression')[0]).toggle();
  28. $(el.find('.button_alternate_expression')[0]).toggleClass('unlock').toggleClass('lock');
  29. }
  30. } catch (e) {
  31. command.lockexpression = false;
  32. }
  33. }
  34. }
  35. } else {
  36. el.find('.expression_elements').remove();
  37. command.variable_value_menu = null;
  38. }
  39. addHandlers(command, function_obj, el);
  40. return el;
  41. }
  42. function addHandlers (command, function_obj, return_dom) {
  43. return_dom.find('.button_alternate_expression').on('click', function() {
  44. if (command.variable_value_menu) {
  45. var text = CodeGenerator.elementExpressionCode(command.variable_value_menu);
  46. if (text) {
  47. $(return_dom.find('.expression_elements')[0]).toggle();
  48. $(return_dom.find('.textual_expression')[0]).text(text);
  49. $(return_dom.find('.textual_expression')[0]).toggle();
  50. $(this).toggleClass('unlock').toggleClass('lock');
  51. command.lockexpression = !command.lockexpression;
  52. }
  53. }
  54. });
  55. return_dom.find('.button_remove_command').on('click', function() {
  56. if (CommandsManagement.removeCommand(command, function_obj, return_dom)) {
  57. return_dom.fadeOut(400, function() {
  58. return_dom.remove();
  59. });
  60. }
  61. });
  62. }