writer.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 VariableValueMenu from './variable_value_menu';
  8. import * as VariableValueMenuManagement from './variable_value_menu';
  9. import * as CommandsManagement from '../commands';
  10. export function createFloatingCommand () {
  11. return $('<div class="ui writer created_element"> <i class="ui icon small upload"></i> <span> '+LocalizedStrings.getUI('text_command_write')+' var </span></div>');
  12. }
  13. export function renderCommand (command, function_obj) {
  14. var ret = '';
  15. ret += '<div class="ui writer command_container"> <i class="ui icon small upload command_drag"></i> <i class="ui icon times red button_remove_command"></i> <span class="span_command_spec">'+LocalizedStrings.getUI('text_command_write')+' ( </span><div class="all_elements_write"></div> <span class="close_parentheses span_command_spec">)</span> </div>';
  16. var el = $(ret);
  17. el.data('command', command);
  18. for (var i = 0; i < command.content.length; i ++) {
  19. var new_div_item = $( '<div class="var_value_menu_div"></div>' );
  20. el.find('.all_elements_write').append(new_div_item);
  21. VariableValueMenu.renderMenu(command, command.content[i], new_div_item, function_obj);
  22. addHandlerIconAdd(el.find('.all_elements_write'), command, function_obj);
  23. }
  24. addHandlers(command, function_obj, el);
  25. return el;
  26. }
  27. function addHandlers (command, function_obj, writer_dom) {
  28. writer_dom.find('.button_remove_command').on('click', function() {
  29. if (CommandsManagement.removeCommand(command, function_obj, writer_dom)) {
  30. writer_dom.remove();
  31. }
  32. });
  33. }
  34. function addHandlerIconAdd (dom_object, command, function_obj, insert_after = false, after_which = null) {
  35. var icon_add_item = $( '<i class="ui icon plus square outline icon_add_item_to_writer"></i> ' );
  36. if (!insert_after) {
  37. dom_object.append(icon_add_item);
  38. } else {
  39. icon_add_item.insertAfter(after_which);
  40. }
  41. icon_add_item.on('click', function(e) {
  42. var new_div_item = $( '<div class="var_value_menu_div" style="display:none;"></div>' );
  43. new_div_item.insertAfter(icon_add_item);
  44. var new_related_menu = new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true);
  45. VariableValueMenu.renderMenu(command, new_related_menu, new_div_item, function_obj);
  46. addHandlerIconAdd(dom_object, command, function_obj, true, new_div_item);
  47. var pos = 1;
  48. dom_object.find('.icon_add_item_to_writer').each(function() {
  49. if ($(this).get(0) === icon_add_item.get(0)) {
  50. command.content.splice(pos, 0, new_related_menu);
  51. } else {
  52. pos ++;
  53. }
  54. });
  55. new_div_item.fadeIn();
  56. });
  57. }
  58. export function addContent (command, ref_object, dom_object, menu_var_or_value, function_obj, ref_object_content) {
  59. if (dom_object.hasClass('var_value_menu_div')) {
  60. var icon_add_item = $( '<i class="ui icon plus square outline icon_add_item_to_writer"></i> ' );
  61. icon_add_item.insertAfter(dom_object);
  62. icon_add_item.on('click', function(e) {
  63. var new_div_item = $( '<div class="var_value_menu_div"></div>' );
  64. new_div_item.insertAfter(icon_add_item);
  65. var new_related_menu = new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true);
  66. VariableValueMenu.renderMenu(command, new_related_menu, new_div_item, function_obj);
  67. command.content.push(new_related_menu);
  68. });
  69. }
  70. }