writer.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import * as Models from '../ivprog_elements';
  2. import { LocalizedStrings } from '../../services/localizedStringsService';
  3. import * as VariableValueMenu from './variable_value_menu';
  4. import * as VariableValueMenuManagement from './variable_value_menu';
  5. import * as CommandsManagement from '../commands';
  6. export function createFloatingCommand () {
  7. return $('<div class="ui writer created_element"> <i class="ui icon small upload"></i> <span> '+LocalizedStrings.getUI('text_command_write')+' var </span></div>');
  8. }
  9. export function renderCommand (command, function_obj) {
  10. var ret = '';
  11. 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>';
  12. var el = $(ret);
  13. el.data('command', command);
  14. for (var i = 0; i < command.content.length; i ++) {
  15. var new_div_item = $( '<div class="var_value_menu_div"></div>' );
  16. var div_parent_with_handler = $( '<div class="div_parent_handler"></div>' );
  17. div_parent_with_handler.append($('<i class="ui icon ellipsis vertical inverted handler"></i>'));
  18. div_parent_with_handler.append(new_div_item);
  19. div_parent_with_handler.append($('<i class="white inverted icon times handler"></i>'));
  20. el.find('.all_elements_write').append(div_parent_with_handler);
  21. VariableValueMenu.renderMenu(command, command.content[i], new_div_item, function_obj);
  22. addHandlerIconAdd(el.find('.all_elements_write'), command, function_obj);
  23. addHandlersManager(command, function_obj, el, div_parent_with_handler, command.content[i]);
  24. }
  25. if (command.content.length == 0) {
  26. addHandlerIconAdd(el.find('.all_elements_write'), command, function_obj);
  27. }
  28. addHandlers(command, function_obj, el);
  29. return el;
  30. }
  31. function addHandlersManager (command, function_obj, writer_dom, item_div, content_element) {
  32. item_div.find('.times').on('click', function() {
  33. for (var i = 0; i < command.content.length; i++) {
  34. if (command.content[i] == content_element) {
  35. delete command.content[i];
  36. command.content.splice(i, 1);
  37. item_div.children().off();
  38. item_div.off();
  39. item_div.fadeOut();
  40. if (command.content.length > 0) {
  41. item_div.next('.icon_add_item_to_writer').fadeOut();
  42. }
  43. break;
  44. }
  45. }
  46. });
  47. }
  48. function addHandlers (command, function_obj, writer_dom) {
  49. writer_dom.find('.button_remove_command').on('click', function() {
  50. if (CommandsManagement.removeCommand(command, function_obj, writer_dom)) {
  51. writer_dom.fadeOut(400, function() {
  52. writer_dom.remove();
  53. });
  54. }
  55. });
  56. Sortable.create(writer_dom.find(".all_elements_write")[0], {
  57. handle: '.ellipsis',
  58. animation: 100,
  59. ghostClass: 'ghost',
  60. group: 'writer_' + Math.floor(Math.random() * 10000000),
  61. draggable: '.div_parent_handler',
  62. onEnd: function (evt) {
  63. command.content.splice(evt.newIndex, 0, command.content.splice(evt.oldIndex, 1)[0]);
  64. writer_dom.empty();
  65. writer_dom.replaceWith(renderCommand(command, function_obj));
  66. }
  67. });
  68. }
  69. function addHandlerIconAdd (dom_object, command, function_obj, insert_after = false, after_which = null) {
  70. var icon_add_item = $( '<i class="ui icon plus square outline icon_add_item_to_writer"></i> ' );
  71. if (!insert_after) {
  72. dom_object.append(icon_add_item);
  73. } else {
  74. icon_add_item.insertAfter(after_which);
  75. }
  76. icon_add_item.on('click', function(e) {
  77. var div_parent_with_handler = $( '<div class="div_parent_handler" style="display:none;"></div>' );
  78. var new_div_item = $( '<div class="var_value_menu_div"></div>' );
  79. div_parent_with_handler.append($('<i class="ui icon ellipsis vertical inverted handler"></i>'));
  80. div_parent_with_handler.append(new_div_item);
  81. div_parent_with_handler.append($('<i class="white inverted icon times handler"></i>'));
  82. div_parent_with_handler.insertAfter(icon_add_item);
  83. var new_related_menu = new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true);
  84. VariableValueMenu.renderMenu(command, new_related_menu, new_div_item, function_obj);
  85. addHandlerIconAdd(dom_object, command, function_obj, true, div_parent_with_handler);
  86. addHandlersManager(command, function_obj, dom_object, div_parent_with_handler, new_related_menu);
  87. var pos = 1;
  88. dom_object.find('.icon_add_item_to_writer').each(function() {
  89. if ($(this).get(0) === icon_add_item.get(0)) {
  90. command.content.splice(pos, 0, new_related_menu);
  91. } else {
  92. pos ++;
  93. }
  94. });
  95. if (command.content.length == 1) {
  96. icon_add_item.remove();
  97. }
  98. div_parent_with_handler.fadeIn();
  99. });
  100. }
  101. export function addContent (command, ref_object, dom_object, menu_var_or_value, function_obj, ref_object_content) {
  102. if (dom_object.hasClass('var_value_menu_div')) {
  103. var icon_add_item = $( '<i class="ui icon plus square outline icon_add_item_to_writer"></i> ' );
  104. icon_add_item.insertAfter(dom_object);
  105. icon_add_item.on('click', function(e) {
  106. var div_parent_with_handler = $( '<div class="div_parent_handler"></div>' );
  107. div_parent_with_handler.append($('<i class="ui icon ellipsis vertical inverted handler"></i>'));
  108. div_parent_with_handler.append(new_div_item);
  109. div_parent_with_handler.append($('<i class="white inverted icon times handler"></i>'));
  110. div_parent_with_handler.insertAfter(icon_add_item);
  111. var new_related_menu = new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true);
  112. VariableValueMenu.renderMenu(command, new_related_menu, new_div_item, function_obj);
  113. addHandlersManager(command, function_obj, dom_object, div_parent_with_handler, new_related_menu);
  114. command.content.push(new_related_menu);
  115. if (command.content.length == 1) {
  116. icon_add_item.remove();
  117. }
  118. });
  119. }
  120. }