writer.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. import * as GenericExpressionManagement from "./generic_expression";
  7. export function createFloatingCommand () {
  8. return $(
  9. '<div class="ui writer created_element"> <i class="ui icon small upload"></i> <span> ' +
  10. LocalizedStrings.getUI("text_command_write") +
  11. " var </span></div>"
  12. );
  13. }
  14. export function renderCommand (command, function_obj) {
  15. const ret = `<div class="ui writer command_container">
  16. <i class="ui icon small upload command_drag"></i>
  17. <i class="ui icon times red button_remove_command"></i>
  18. <span class="span_command_spec">${LocalizedStrings.getUI(
  19. "text_command_write"
  20. )}
  21. ( </span><div class="all_elements_write"></div>
  22. <span class="close_parentheses span_command_spec">)</span>
  23. <img data-state="${command.newline ? "on" : "false"}" src="${
  24. command.newline ? "img/new_line.svg" : "img/no_new_line.svg"
  25. }" class="ivprog_newline_btn"/>
  26. </div>`;
  27. const el = $(ret);
  28. el.data("command", command);
  29. //renderExpression (command, function_obj, div_to_render, expression_array)
  30. GenericExpressionManagement.renderExpression(
  31. command,
  32. function_obj,
  33. el.find(".all_elements_write"),
  34. command.content
  35. );
  36. /*for (var i = 0; i < command.content.length; i ++) {
  37. var new_div_item = $( '<div class="var_value_menu_div"></div>' );
  38. var div_parent_with_handler = $( '<div class="div_parent_handler"></div>' );
  39. div_parent_with_handler.append($('<i class="ui icon ellipsis vertical inverted handler"></i>'));
  40. div_parent_with_handler.append(new_div_item);
  41. div_parent_with_handler.append($('<i class="white inverted icon times handler"></i>'));
  42. el.find('.all_elements_write').append(div_parent_with_handler);
  43. VariableValueMenu.renderMenu(command, command.content[i], new_div_item, function_obj);
  44. addHandlerIconAdd(el.find('.all_elements_write'), command, function_obj);
  45. addHandlersManager(command, function_obj, el, div_parent_with_handler, command.content[i]);
  46. }
  47. if (command.content.length == 0) {
  48. addHandlerIconAdd(el.find('.all_elements_write'), command, function_obj);
  49. }*/
  50. addHandlers(command, function_obj, el);
  51. return el;
  52. }
  53. function addHandlersManager (
  54. command,
  55. _function_obj,
  56. _writer_dom,
  57. item_div,
  58. content_element
  59. ) {
  60. item_div.find(".times").on("click", function () {
  61. for (let i = 0; i < command.content.length; i++) {
  62. if (command.content[i] == content_element) {
  63. delete command.content[i];
  64. command.content.splice(i, 1);
  65. item_div.children().off();
  66. item_div.off();
  67. item_div.fadeOut();
  68. if (command.content.length > 0) {
  69. item_div.next(".icon_add_item_to_writer").fadeOut();
  70. }
  71. break;
  72. }
  73. }
  74. });
  75. }
  76. function addHandlers (command, function_obj, writer_dom) {
  77. writer_dom.find(".button_remove_command").on("click", function () {
  78. if (CommandsManagement.removeCommand(command, function_obj, writer_dom)) {
  79. writer_dom.fadeOut(400, function () {
  80. writer_dom.remove();
  81. });
  82. }
  83. });
  84. Sortable.create(writer_dom.find(".all_elements_write")[0], {
  85. handle: ".ellipsis",
  86. animation: 100,
  87. ghostClass: "ghost",
  88. group: "writer_" + Math.floor(Math.random() * 10000000),
  89. draggable: ".div_parent_handler",
  90. onEnd: function (evt) {
  91. command.content.splice(
  92. evt.newIndex,
  93. 0,
  94. command.content.splice(evt.oldIndex, 1)[0]
  95. );
  96. writer_dom.empty();
  97. writer_dom.replaceWith(renderCommand(command, function_obj));
  98. },
  99. });
  100. // Attach event handles for the newline button
  101. const newlineBtn = writer_dom.find(".ivprog_newline_btn");
  102. newlineBtn.on("click", function () {
  103. const state = this.dataset.state;
  104. if (state === "on") {
  105. this.dataset.state = "off";
  106. command.newline = false;
  107. this.setAttribute("src", "img/no_new_line.svg");
  108. } else {
  109. this.dataset.state = "on";
  110. command.newline = true;
  111. this.setAttribute("src", "img/new_line.svg");
  112. }
  113. writer_dom.data("command", command);
  114. setPopup(newlineBtn, command.newline);
  115. });
  116. // Attach jquery popup
  117. setPopup(newlineBtn, command.newline);
  118. }
  119. function setPopup (element, state) {
  120. if (element.popup("exists")) {
  121. element.popup("destroy");
  122. }
  123. const content = state
  124. ? LocalizedStrings.getUI("write_command_newline_on")
  125. : LocalizedStrings.getUI("write_command_newline_off");
  126. element.popup({
  127. content: content,
  128. delay: {
  129. show: 750,
  130. hide: 0,
  131. },
  132. });
  133. }
  134. function _addHandlerIconAdd (
  135. dom_object,
  136. command,
  137. function_obj,
  138. insert_after = false,
  139. after_which = null
  140. ) {
  141. const icon_add_item = $(
  142. '<i class="ui icon plus square outline icon_add_item_to_writer"></i> '
  143. );
  144. if (!insert_after) {
  145. dom_object.append(icon_add_item);
  146. } else {
  147. icon_add_item.insertAfter(after_which);
  148. }
  149. icon_add_item.on("click", function (_event) {
  150. const div_parent_with_handler = $(
  151. '<div class="div_parent_handler" style="display:none;"></div>'
  152. );
  153. const new_div_item = $('<div class="var_value_menu_div"></div>');
  154. div_parent_with_handler.append(
  155. $('<i class="ui icon ellipsis vertical inverted handler"></i>')
  156. );
  157. div_parent_with_handler.append(new_div_item);
  158. div_parent_with_handler.append(
  159. $('<i class="white inverted icon times handler"></i>')
  160. );
  161. div_parent_with_handler.insertAfter(icon_add_item);
  162. const new_related_menu = new Models.VariableValueMenu(
  163. VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all,
  164. null,
  165. null,
  166. null,
  167. true
  168. );
  169. VariableValueMenu.renderMenu(
  170. command,
  171. new_related_menu,
  172. new_div_item,
  173. function_obj
  174. );
  175. addHandlerIconAdd(
  176. dom_object,
  177. command,
  178. function_obj,
  179. true,
  180. div_parent_with_handler
  181. );
  182. addHandlersManager(
  183. command,
  184. function_obj,
  185. dom_object,
  186. div_parent_with_handler,
  187. new_related_menu
  188. );
  189. let pos = 1;
  190. dom_object.find(".icon_add_item_to_writer").each(function () {
  191. if ($(this).get(0) === icon_add_item.get(0)) {
  192. command.content.splice(pos, 0, new_related_menu);
  193. } else {
  194. pos++;
  195. }
  196. });
  197. if (command.content.length == 1) {
  198. icon_add_item.remove();
  199. }
  200. div_parent_with_handler.fadeIn();
  201. });
  202. }
  203. export function addContent (
  204. command,
  205. _ref_object,
  206. dom_object,
  207. _menu_var_or_value,
  208. function_obj,
  209. _ref_object_content
  210. ) {
  211. if (dom_object.hasClass("var_value_menu_div")) {
  212. const icon_add_item = $(
  213. '<i class="ui icon plus square outline icon_add_item_to_writer"></i> '
  214. );
  215. icon_add_item.insertAfter(dom_object);
  216. icon_add_item.on("click", function (_event) {
  217. const div_parent_with_handler = $(
  218. '<div class="div_parent_handler"></div>'
  219. );
  220. div_parent_with_handler.append(
  221. $('<i class="ui icon ellipsis vertical inverted handler"></i>')
  222. );
  223. div_parent_with_handler.append(new_div_item);
  224. div_parent_with_handler.append(
  225. $('<i class="white inverted icon times handler"></i>')
  226. );
  227. div_parent_with_handler.insertAfter(icon_add_item);
  228. const new_related_menu = new Models.VariableValueMenu(
  229. VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all,
  230. null,
  231. null,
  232. null,
  233. true
  234. );
  235. VariableValueMenu.renderMenu(
  236. command,
  237. new_related_menu,
  238. new_div_item,
  239. function_obj
  240. );
  241. addHandlersManager(
  242. command,
  243. function_obj,
  244. dom_object,
  245. div_parent_with_handler,
  246. new_related_menu
  247. );
  248. command.content.push(new_related_menu);
  249. if (command.content.length == 1) {
  250. icon_add_item.remove();
  251. }
  252. });
  253. }
  254. }