writer.js 6.0 KB

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