writer.js 6.3 KB

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