whiletrue.js 7.0 KB

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