iftrue.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import { LocalizedStrings } from '../../services/localizedStringsService';
  2. import * as CommandsManagement from '../commands';
  3. import * as GenericExpressionManagement from './generic_expression';
  4. import * as CodeGenerator from '../code_generator';
  5. export function createFloatingCommand () {
  6. return $('<div class="ui iftrue created_element"> <i class="ui icon small random"></i> <span> if (x < 1) { } </span></div>');
  7. }
  8. export function renderCommand (command, function_obj) {
  9. var ret = '';
  10. ret += '<div class="ui iftrue command_container"><div class="ui data_block_if" data-if="true"> <i class="ui icon small random command_drag"></i> <i class="ui icon times red button_remove_command"></i> <button class="ui icon button minimize_block_button tiny"><i class="icon window minimize"></i></button>';
  11. ret += '<span class="span_command_spec"> ' + LocalizedStrings.getUI('text_if') + '</span>';
  12. ret += ' <span class="span_command_spec"> ( </span> <div class="conditional_expression"></div> <span class="textual_expression"></span> <span class="span_command_spec"> ) </span> ';
  13. ret += ' <i class="ui icon i cursor button_write_expression" style="margin-right: 1rem !important;"></i> ';
  14. ret += '<i class="ui icon unlock button_alternate_expression"></i>';
  15. ret += '<span> </span> ';
  16. ret += '<div class="ui block_commands commands_if conditional_comands_block" data-if="true">';
  17. ret += '</div></div>';
  18. ret += '<div class="ui data_block_else" data-else="true"> <span class="span_command_spec"> ' + LocalizedStrings.getUI('text_else') + ' </span>';
  19. ret += '<div class="ui block_commands commands_else conditional_comands_block" data-else="true">';
  20. ret += '</div>';
  21. ret += '<span></span></div>';
  22. ret += '</div>';
  23. var el = $(ret);
  24. el.data('command', command);
  25. el.find('.block_commands').data('command', command);
  26. el.find('.data_block_if').data('command', command);
  27. el.find('.data_block_else').data('command', command);
  28. el.find('.commands_if').data('command', command);
  29. addHandlers(command, function_obj, el);
  30. //ConditionalExpressionManagement.renderExpression(command, command.expression, function_obj, el.find('.conditional_expression'));
  31. GenericExpressionManagement.renderExpression(command, function_obj, el.find('.conditional_expression'), command.expression);
  32. if (command.commands_block) {
  33. for (var j = 0; j < command.commands_block.length; j++) {
  34. CommandsManagement.renderCommand(command.commands_block[j], $(el.find('.commands_if')[0]), 3, function_obj);
  35. }
  36. }
  37. if (command.commands_else) {
  38. for (var j = 0; j < command.commands_else.length; j++) {
  39. CommandsManagement.renderCommand(command.commands_else[j], $(el.find('.commands_else')[0]), 3, function_obj);
  40. }
  41. }
  42. if (command.collapsed) {
  43. el.children('.data_block_else').toggle();
  44. $(el.find('.block_commands')[0]).toggle();
  45. }
  46. if (command.lockexpression) {
  47. if (command.expression) {
  48. try {
  49. var text = CodeGenerator.elementExpressionCode(command.expression);
  50. if (text) {
  51. $(el.find('.conditional_expression')[0]).toggle();
  52. $(el.find('.textual_expression')[0]).text(text);
  53. $(el.find('.textual_expression')[0]).toggle();
  54. $(el.find('.button_alternate_expression')[0]).toggleClass('unlock').toggleClass('lock');
  55. }
  56. } catch (e) {
  57. command.lockexpression = false;
  58. }
  59. }
  60. }
  61. el.find('.unlock').popup({
  62. content : LocalizedStrings.getUI("text_lock_expression"),
  63. delay: {
  64. show: 750,
  65. hide: 0
  66. }
  67. });
  68. el.find('.lock').popup({
  69. content : LocalizedStrings.getUI("text_unlock_expression"),
  70. delay: {
  71. show: 750,
  72. hide: 0
  73. }
  74. });
  75. el.find('.button_write_expression').popup({
  76. content : LocalizedStrings.getUI("text_edit_expression"),
  77. delay: {
  78. show: 750,
  79. hide: 0
  80. }
  81. });
  82. return el;
  83. }
  84. function addHandlers (command, function_obj, iftrue_dom) {
  85. iftrue_dom.find('.button_write_expression').on('click', function() {
  86. window.expressionEdition = true;
  87. window.inputExpression = null;
  88. var afterWhichElement;
  89. var lockButton = $(iftrue_dom.find('.button_alternate_expression')[0]);
  90. var editButton = $(this);
  91. afterWhichElement = $(iftrue_dom.find('.conditional_expression')[0]);
  92. if (command.lockexpression) {
  93. afterWhichElement = $(iftrue_dom.find('.textual_expression')[0]);
  94. }
  95. var text = "";
  96. if (command.expression) {
  97. if (command.expression.length == 1 && command.expression[0].content == null && !command.expression[0].function_called) {
  98. text = "";
  99. } else {
  100. try {
  101. text = CodeGenerator.elementExpressionCode(command.expression);
  102. } catch(ex) {
  103. text = "";
  104. }
  105. }
  106. }
  107. var ok_button = $('<i class="ui icon check circle expression-edit-confirm"></i>');
  108. var cancel_button = $('<i class="ui icon undo expression-edit-cancel"></i>');
  109. var input = $('<input type="text" spellcheck="false" autocomplete="off" class="input-expression-field" >');
  110. input.val(text);
  111. input.focusout(function(evt) {
  112. ok_button.click();
  113. evt.preventDefault();
  114. return true;
  115. });
  116. input.keyup(function(evt) {
  117. if (evt.keyCode == 27) { // esc
  118. cancel_button.click();
  119. }
  120. if (evt.keyCode == 13) { // enter
  121. ok_button.click();
  122. }
  123. });
  124. ok_button.click(function() {
  125. var parsed = null;
  126. parsed = GenericExpressionManagement.expressionParserToVisual(input.val(), function_obj, input);
  127. if (parsed) {
  128. window.expressionEdition = false;
  129. command.expression = parsed;
  130. renderAlgorithm();
  131. }
  132. });
  133. cancel_button.mousedown(function(evt) {
  134. var parsed = GenericExpressionManagement.expressionParserToVisual(text, function_obj, input);
  135. if (parsed) {
  136. window.expressionEdition = false;
  137. command.expression = parsed;
  138. renderAlgorithm();
  139. }
  140. });
  141. input.insertAfter(afterWhichElement);
  142. input.focus();
  143. cancel_button.insertAfter(input);
  144. ok_button.insertAfter(input);
  145. var len = text.length;
  146. input[0].setSelectionRange(len, len);
  147. afterWhichElement.css('display', 'none');
  148. lockButton.css('display', 'none');
  149. editButton.css('display', 'none');
  150. ok_button.popup({
  151. content : LocalizedStrings.getUI("text_edit_expression_confirm"),
  152. delay: {
  153. show: 750,
  154. hide: 0
  155. }
  156. });
  157. cancel_button.popup({
  158. content : LocalizedStrings.getUI("text_edit_expression_cancel"),
  159. delay: {
  160. show: 750,
  161. hide: 0
  162. }
  163. });
  164. });
  165. $(iftrue_dom.find('.textual_expression')[0]).toggle();
  166. iftrue_dom.find('.button_alternate_expression').on('click', function() {
  167. if (command.expression) {
  168. var text = CodeGenerator.elementExpressionCode(command.expression);
  169. if (text) {
  170. $(iftrue_dom.find('.conditional_expression')[0]).toggle();
  171. $(iftrue_dom.find('.textual_expression')[0]).text(text);
  172. $(iftrue_dom.find('.textual_expression')[0]).toggle();
  173. $(this).toggleClass('unlock').toggleClass('lock');
  174. command.lockexpression = !command.lockexpression;
  175. }
  176. }
  177. if (command.lockexpression) {
  178. iftrue_dom.find('.lock').popup({
  179. content : LocalizedStrings.getUI("text_unlock_expression"),
  180. delay: {
  181. show: 750,
  182. hide: 0
  183. }
  184. });
  185. } else {
  186. iftrue_dom.find('.unlock').popup({
  187. content : LocalizedStrings.getUI("text_lock_expression"),
  188. delay: {
  189. show: 750,
  190. hide: 0
  191. }
  192. });
  193. }
  194. });
  195. iftrue_dom.find('.button_remove_command').on('click', function() {
  196. if (CommandsManagement.removeCommand(command, function_obj, iftrue_dom)) {
  197. iftrue_dom.fadeOut(400, function() {
  198. iftrue_dom.remove();
  199. });
  200. }
  201. });
  202. iftrue_dom.find('.minimize_block_button').on('click', function() {
  203. iftrue_dom.children('.data_block_else').toggle();
  204. $(iftrue_dom.find('.block_commands')[0]).toggle();
  205. command.collapsed = !command.collapsed;
  206. });
  207. }