writer.js 8.2 KB

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