contextualized_menu.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 CommandsManagement from '../commands';
  6. import * as VariableValueMenuManagement from './variable_value_menu';
  7. import * as SwitchManagement from './switch';
  8. import * as RepeatNTimesManagement from './repeatNtimes';
  9. export function renderMenu (command, dom_where_render, function_obj, dom_command) {
  10. var menu_div = '<div class="ui dropdown menu_commands" ><i class="icon code"></i> <div class="menu"> ';
  11. if ((command.type == Models.COMMAND_TYPES.repeatNtimes)
  12. || (command.type == Models.COMMAND_TYPES.whiletrue)
  13. || (command.type == Models.COMMAND_TYPES.dowhiletrue)) {
  14. menu_div += '<a class="item" data-command="'+Models.COMMAND_TYPES.break+'"><i class="download icon"></i> '+LocalizedStrings.getUI('btn_break')+' </a>';
  15. } else {
  16. menu_div += '<a class="item" data-command="'+Models.COMMAND_TYPES.break+'"><i class="download icon"></i> '+LocalizedStrings.getUI('btn_break')+' </a>';
  17. menu_div += '<a class="item" data-command="'+Models.COMMAND_TYPES.switchcase+'"><i class="download icon"></i> '+LocalizedStrings.getUI('btn_case')+' </a>';
  18. }
  19. menu_div += '</div></div>';
  20. menu_div = $(menu_div);
  21. dom_where_render.append(menu_div);
  22. addHandlers(command, dom_where_render, function_obj, dom_command);
  23. }
  24. function addHandlers (command, dom_where_render, function_obj, dom_command) {
  25. dom_where_render.find('.menu_commands').dropdown({
  26. on: 'hover'
  27. });
  28. dom_where_render.find('.menu_commands a').on('click', function(evt){
  29. if ((command.type == Models.COMMAND_TYPES.repeatNtimes)
  30. || (command.type == Models.COMMAND_TYPES.whiletrue)
  31. || (command.type == Models.COMMAND_TYPES.dowhiletrue)) {
  32. if (command.commands_block == null || command.commands_block.length == 0) {
  33. command.commands_block = [];
  34. var new_cmd = CommandsManagement.genericCreateCommand($(this).data('command'));
  35. command.commands_block.push(new_cmd);
  36. CommandsManagement.renderCommand(new_cmd, dom_command.find('.block_commands'), 3, function_obj);
  37. } else {
  38. CommandsManagement.createFloatingCommand(function_obj, dom_command.find('.block_commands'), $(this).data('command'), evt);
  39. }
  40. } else {
  41. switch ($(this).data('command')) {
  42. case Models.COMMAND_TYPES.break:
  43. CommandsManagement.createFloatingCommand(function_obj, dom_command.find('.block_commands'), $(this).data('command'), evt);
  44. break;
  45. case Models.COMMAND_TYPES.switchcase:
  46. addCaseToSwitch(command, dom_where_render, function_obj, dom_command);
  47. break;
  48. }
  49. }
  50. });
  51. }
  52. function addCaseToSwitch (command, dom_where_render, function_obj, dom_command) {
  53. if ((command.cases == null)) {
  54. command.cases = [];
  55. }
  56. var sc = new Models.SwitchCase(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true));
  57. command.cases.push(sc);
  58. SwitchManagement.renderCase(sc, command, function_obj, dom_command.find('.all_cases_div'));
  59. }