writer.js 9.0 KB

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