dowhiletrue.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 dowhiletrue created_element"> <i class="ui icon small sync"></i> <span> '+ LocalizedStrings.getUI('text_command_do') +' <br> ' + LocalizedStrings.getUI('text_command_do_until') +'(x < 10) </span></div>');
  9. }
  10. export function renderCommand (command, function_obj) {
  11. var ret = '';
  12. ret += '<div class="ui dowhiletrue 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_do') + ' </span>';
  13. ret += '<div class="ui block_commands" data-subblock="" data-idcommand="">';
  14. ret += '</div>';
  15. ret += ' <span class="span_command_spec"> ' + LocalizedStrings.getUI('text_command_do_until') + ' </span> <span class="span_command_spec"> ( </span> <div class="conditional_expression"></div> <span class="textual_expression"></span> <span class="span_command_spec"> ) </span>';
  16. ret += ' <i class="ui icon i cursor button_write_expression" style="display: inline !important;"></i> <i class="ui icon unlock button_alternate_expression" style="margin-left: 1rem !important;"></i> <div class="ui context_menu"></div> ';
  17. ret += '</div>';
  18. var el = $(ret);
  19. el.data('command', command);
  20. el.find('.block_commands').data('command', command);
  21. addHandlers(command, function_obj, el);
  22. ContextualizedMenu.renderMenu(command, el.find('.context_menu'), function_obj, el);
  23. //ConditionalExpressionManagement.renderExpression(command, command.expression, function_obj, el.find('.conditional_expression'));
  24. if (command.expression) {
  25. GenericExpressionManagement.renderExpression(command, function_obj, el.find('.conditional_expression'), command.expression);
  26. }
  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')[el.find('.conditional_expression').length - 1]).toggle();
  38. $(el.find('.textual_expression')[el.find('.textual_expression').length - 1]).text(text);
  39. $(el.find('.textual_expression')[el.find('.textual_expression').length - 1]).toggle();
  40. $(el.find('.button_alternate_expression')[el.find('.button_alternate_expression').length - 1]).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, dowhiletrue_dom) {
  71. dowhiletrue_dom.find('.button_write_expression').on('click', function() {
  72. window.expressionEdition = true;
  73. window.inputExpression = null;
  74. var afterWhichElement;
  75. var lockButton = $(dowhiletrue_dom.find('.button_write_expression')[dowhiletrue_dom.find('.button_write_expression').length - 1]);
  76. var editButton = $(this);
  77. afterWhichElement = $(dowhiletrue_dom.find('.conditional_expression')[dowhiletrue_dom.find('.conditional_expression').length - 1]);
  78. if (command.lockexpression) {
  79. afterWhichElement = $(dowhiletrue_dom.find('.textual_expression')[dowhiletrue_dom.find('.textual_expression').length - 1]);
  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. ok_button.click();
  99. evt.preventDefault();
  100. return true;
  101. });
  102. input.keyup(function(evt) {
  103. if (evt.keyCode == 27) { // esc
  104. cancel_button.click();
  105. }
  106. if (evt.keyCode == 13) { // enter
  107. ok_button.click();
  108. }
  109. });
  110. ok_button.click(function() {
  111. var parsed = null;
  112. parsed = GenericExpressionManagement.expressionParserToVisual(input.val(), function_obj, input);
  113. if (parsed) {
  114. window.expressionEdition = false;
  115. command.expression = parsed;
  116. renderAlgorithm();
  117. }
  118. });
  119. cancel_button.mousedown(function(evt) {
  120. var parsed = GenericExpressionManagement.expressionParserToVisual(text, function_obj, input);
  121. if (parsed) {
  122. window.expressionEdition = false;
  123. command.expression = parsed;
  124. renderAlgorithm();
  125. }
  126. });
  127. input.insertAfter(afterWhichElement);
  128. input.focus();
  129. cancel_button.insertAfter(input);
  130. ok_button.insertAfter(input);
  131. var len = text.length;
  132. input[0].setSelectionRange(len, len);
  133. afterWhichElement.css('display', 'none');
  134. lockButton.css('display', 'none');
  135. editButton.css('display', 'none');
  136. ok_button.popup({
  137. content : LocalizedStrings.getUI("text_edit_expression_confirm"),
  138. delay: {
  139. show: 750,
  140. hide: 0
  141. }
  142. });
  143. cancel_button.popup({
  144. content : LocalizedStrings.getUI("text_edit_expression_cancel"),
  145. delay: {
  146. show: 750,
  147. hide: 0
  148. }
  149. });
  150. });
  151. dowhiletrue_dom.find('.minimize_block_button').on('click', function(evt){
  152. dowhiletrue_dom.children('.ui.block_commands').toggle();
  153. command.collapsed = !command.collapsed;
  154. });
  155. $(dowhiletrue_dom.find('.textual_expression')[0]).toggle();
  156. dowhiletrue_dom.find('.button_alternate_expression').on('click', function() {
  157. if (command.expression) {
  158. var text = CodeGenerator.elementExpressionCode(command.expression);
  159. if (text) {
  160. $(dowhiletrue_dom.find('.conditional_expression')[dowhiletrue_dom.find('.conditional_expression').length - 1]).toggle();
  161. $(dowhiletrue_dom.find('.textual_expression')[dowhiletrue_dom.find('.textual_expression').length - 1]).text(text);
  162. $(dowhiletrue_dom.find('.textual_expression')[dowhiletrue_dom.find('.textual_expression').length - 1]).toggle();
  163. $(this).toggleClass('unlock').toggleClass('lock');
  164. command.lockexpression = !command.lockexpression;
  165. }
  166. }
  167. if (command.lockexpression) {
  168. dowhiletrue_dom.find('.lock').popup({
  169. content : LocalizedStrings.getUI("text_unlock_expression"),
  170. delay: {
  171. show: 750,
  172. hide: 0
  173. }
  174. });
  175. } else {
  176. dowhiletrue_dom.find('.unlock').popup({
  177. content : LocalizedStrings.getUI("text_lock_expression"),
  178. delay: {
  179. show: 750,
  180. hide: 0
  181. }
  182. });
  183. }
  184. });
  185. dowhiletrue_dom.find('.button_remove_command').on('click', function() {
  186. if (CommandsManagement.removeCommand(command, function_obj, dowhiletrue_dom)) {
  187. dowhiletrue_dom.fadeOut(400, function() {
  188. dowhiletrue_dom.remove();
  189. });
  190. }
  191. });
  192. }