switch.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 VariableValueMenu from './variable_value_menu';
  9. import * as ContextualizedMenu from './contextualized_menu';
  10. export function createFloatingCommand () {
  11. 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>');
  12. }
  13. export function renderCommand (command, function_obj) {
  14. var ret = '';
  15. 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>';
  16. ret += '</div>';
  17. var el = $(ret);
  18. el.data('command', command);
  19. addHandlers(command, function_obj, el);
  20. ContextualizedMenu.renderMenu(command, el.find('.context_menu'), function_obj, el);
  21. VariableValueMenu.renderMenu(command, command.variable, el.find('.variable_to_switch'), function_obj);
  22. if (command.cases) {
  23. for (var i = 0; i < command.cases.length; i++) {
  24. renderCase(command.cases[i], command, function_obj, el.find('.all_cases_div'));
  25. }
  26. }
  27. return el;
  28. }
  29. export function renderCase (switchcase, command, function_obj, el) {
  30. 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>');
  31. VariableValueMenu.renderMenu(command, switchcase.variable_value_menu, casediv.find('.variable_case'), function_obj);
  32. casediv.data('switchcase', switchcase);
  33. casediv.find('.case_commands_block').data('switchcase', switchcase);
  34. el.append(casediv);
  35. if (switchcase.commands_block) {
  36. for (var j = 0; j < switchcase.commands_block.length; j++) {
  37. CommandsManagement.renderCommand(switchcase.commands_block[j], $(casediv.find('.case_commands_block')[0]), 3, function_obj);
  38. }
  39. }
  40. casediv.find('.button_remove_command').on('click', function() {
  41. for (var i = 0; i < command.cases.length; i++) {
  42. if (switchcase == command.cases[i]) {
  43. delete command.cases[i];
  44. command.cases.splice(i, 1);
  45. casediv.remove();
  46. break;
  47. }
  48. }
  49. });
  50. }
  51. function addHandlers (command, function_obj, switch_dom) {
  52. switch_dom.find('.button_remove_command').on('click', function() {
  53. if (CommandsManagement.removeCommand(command, function_obj, switch_dom)) {
  54. switch_dom.fadeOut(400, function() {
  55. switch_dom.remove();
  56. });
  57. }
  58. });
  59. }