1
0

switch.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { LocalizedStrings } from '../../services/localizedStringsService';
  2. import * as CommandsManagement from '../commands';
  3. import * as VariableValueMenu from './variable_value_menu';
  4. import * as ContextualizedMenu from './contextualized_menu';
  5. export function createFloatingCommand () {
  6. return $('<div class="ui switch created_element"> <i class="ui icon small random"></i> <span> '+LocalizedStrings.getUI('text_code_switch')+' ( x ) { <br> '+LocalizedStrings.getUI('text_code_case')+' 1: <br> '+LocalizedStrings.getUI('text_code_case')+' 2: <br> } </span></div>');
  7. }
  8. export function renderCommand (command, function_obj) {
  9. var ret = '';
  10. ret += '<div class="ui switch command_container"> <i class="ui icon small random command_drag" ></i> <i class="ui icon times red button_remove_command"></i> <div class="ui context_menu"></div> <span> '+LocalizedStrings.getUI('text_code_switch')+' ( <div class="ui variable_to_switch"></div> ) <div class="ui all_cases_div"></div></span>';
  11. ret += '</div>';
  12. var el = $(ret);
  13. el.data('command', command);
  14. addHandlers(command, function_obj, el);
  15. ContextualizedMenu.renderMenu(command, el.find('.context_menu'), function_obj, el);
  16. VariableValueMenu.renderMenu(command, command.variable, el.find('.variable_to_switch'), function_obj);
  17. if (command.cases) {
  18. for (var i = 0; i < command.cases.length; i++) {
  19. renderCase(command.cases[i], command, function_obj, el.find('.all_cases_div'));
  20. }
  21. }
  22. return el;
  23. }
  24. export function renderCase (switchcase, command, function_obj, el) {
  25. var casediv = $('<div class="ui case_div"><i class="ui icon times red button_remove_command"></i><span>'+LocalizedStrings.getUI('text_code_case')+'</span> <div class="ui variable_case"></div>: <div class="case_commands_block"></div></div>');
  26. VariableValueMenu.renderMenu(command, switchcase.variable_value_menu, casediv.find('.variable_case'), function_obj);
  27. casediv.data('switchcase', switchcase);
  28. casediv.find('.case_commands_block').data('switchcase', switchcase);
  29. el.append(casediv);
  30. if (switchcase.commands_block) {
  31. for (var j = 0; j < switchcase.commands_block.length; j++) {
  32. CommandsManagement.renderCommand(switchcase.commands_block[j], $(casediv.find('.case_commands_block')[0]), 3, function_obj);
  33. }
  34. }
  35. casediv.find('.button_remove_command').on('click', function() {
  36. for (var i = 0; i < command.cases.length; i++) {
  37. if (switchcase == command.cases[i]) {
  38. delete command.cases[i];
  39. command.cases.splice(i, 1);
  40. casediv.remove();
  41. break;
  42. }
  43. }
  44. });
  45. }
  46. function addHandlers (command, function_obj, switch_dom) {
  47. switch_dom.find('.button_remove_command').on('click', function() {
  48. if (CommandsManagement.removeCommand(command, function_obj, switch_dom)) {
  49. switch_dom.fadeOut(400, function() {
  50. switch_dom.remove();
  51. });
  52. }
  53. });
  54. }