variable_value_menu.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. import $ from 'jquery';
  2. import { Types } from '../types';
  3. import * as Models from '../ivprog_elements';
  4. import { LocalizedStrings } from '../../services/localizedStringsService';
  5. import * as GlobalsManagement from '../globals';
  6. import * as VariablesManagement from '../variables';
  7. import * as AttribuitionsManagement from './attribution';
  8. import * as WritersManagement from './writer';
  9. export const VAR_OR_VALUE_TYPES = Object.freeze({only_variable: 1, only_value: 2, only_function: 3, variable_and_function: 4, variable_and_value_opt: 5,
  10. value_and_function: 6, all: 7});
  11. export function renderMenu (command, ref_object, dom_object, function_obj, size_field = 2) {
  12. var menu_var_or_value = '<div class="ui dropdown menu_var_or_value_dom"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  13. if (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.only_function) {
  14. menu_var_or_value = '<div class="ui dropdown menu_var_or_value_dom"><div class="text"></div><i class="dropdown icon"></i><div class="menu menu_only_functions">';
  15. menu_var_or_value += '</div>';
  16. }
  17. if ((ref_object.variable_and_value == VAR_OR_VALUE_TYPES.variable_and_function)
  18. || (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.value_and_function) || (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.all)) {
  19. menu_var_or_value += '<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_function+'"><i class="dropdown icon"></i>'+LocalizedStrings.getUI('btn_function');
  20. menu_var_or_value += '<div class="menu menu_only_functions">';
  21. menu_var_or_value += '</div></div>';
  22. }
  23. if (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.only_variable) {
  24. menu_var_or_value = '<div class="ui dropdown menu_var_or_value_dom"><div class="text"></div><i class="dropdown icon"></i><div class="menu menu_only_vars">';
  25. menu_var_or_value += '</div>';
  26. }
  27. if ((ref_object.variable_and_value == VAR_OR_VALUE_TYPES.variable_and_function) || (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.variable_and_value_opt) || (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.all)) {
  28. menu_var_or_value += '<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'"><i class="dropdown icon"></i>'+LocalizedStrings.getUI('variable');
  29. menu_var_or_value += '<div class="menu menu_only_vars">';
  30. menu_var_or_value += '</div></div>';
  31. }
  32. if (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.only_value) {
  33. menu_var_or_value = '<input type="text" class="width-dynamic" size="'+size_field+'" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />';
  34. }
  35. if ((ref_object.variable_and_value == VAR_OR_VALUE_TYPES.variable_and_value_opt)
  36. || (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.value_and_function) || (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.all)) {
  37. menu_var_or_value += '<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_value+'">'+LocalizedStrings.getUI('text_value')+'</div>';
  38. }
  39. menu_var_or_value += '</div></div>';
  40. menu_var_or_value = $(menu_var_or_value);
  41. dom_object.append(menu_var_or_value);
  42. addHandlers(command, ref_object, dom_object, menu_var_or_value, function_obj);
  43. addVariablesToMenu(function_obj, menu_var_or_value, ref_object);
  44. addFunctionsToMenu(function_obj, menu_var_or_value, ref_object);
  45. }
  46. function addFunctionsToMenu (function_obj, menu_var_or_value, ref_object) {
  47. var sub_menu = menu_var_or_value.find('.menu_only_functions');
  48. sub_menu.text('');
  49. for (var i = 0; i < window.program_obj.functions.length; i++) {
  50. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_function+'">' + window.program_obj.functions[i].name + ' </div>');
  51. temp.data('function_reference', window.program_obj.functions[i]);
  52. sub_menu.append(temp);
  53. }
  54. }
  55. function addVariablesToMenu (function_obj, menu_var_or_value, ref_object) {
  56. var sub_menu = menu_var_or_value.find('.menu_only_vars');
  57. sub_menu.text('');
  58. if (window.program_obj.globals) {
  59. if (ref_object.include_constant) {
  60. for (var i = 0; i < window.program_obj.globals.length; i++) {
  61. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + window.program_obj.globals[i].name + ' </div>');
  62. temp.data('variable_reference', window.program_obj.globals[i]);
  63. sub_menu.append(temp);
  64. }
  65. } else {
  66. for (var i = 0; i < window.program_obj.globals.length; i++) {
  67. if (!window.program_obj.globals[i].is_constant) {
  68. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + window.program_obj.globals[i].name + ' </div>');
  69. temp.data('variable_reference', window.program_obj.globals[i]);
  70. sub_menu.append(temp);
  71. }
  72. }
  73. }
  74. }
  75. if (function_obj.parameters_list) {
  76. for (var i = 0; i < function_obj.parameters_list.length; i++) {
  77. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + function_obj.parameters_list[i].name + ' </div>');
  78. temp.data('variable_reference', function_obj.parameters_list[i]);
  79. sub_menu.append(temp);
  80. }
  81. }
  82. if (function_obj.variables_list) {
  83. for (var i = 0; i < function_obj.variables_list.length; i++) {
  84. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + function_obj.variables_list[i].name + ' </div>');
  85. temp.data('variable_reference', function_obj.variables_list[i]);
  86. sub_menu.append(temp);
  87. }
  88. }
  89. }
  90. function addHandlers (command, ref_object, dom_object, menu_var_or_value, function_obj) {
  91. if (ref_object.variable_and_value != VAR_OR_VALUE_TYPES.only_value) {
  92. menu_var_or_value.dropdown({
  93. onChange: function(value, text, $selectedItem) {
  94. dom_object.find('.var_name').remove();
  95. switch ($selectedItem.data('option')) {
  96. case VAR_OR_VALUE_TYPES.only_function:
  97. openInputToFunction(command, ref_object, dom_object, menu_var_or_value, function_obj, $($selectedItem).data('function_reference'));
  98. break;
  99. case VAR_OR_VALUE_TYPES.only_value:
  100. openInputToValue(command, ref_object, dom_object, menu_var_or_value, function_obj);
  101. break;
  102. case VAR_OR_VALUE_TYPES.only_variable:
  103. openInputToVariable(command, ref_object, dom_object, menu_var_or_value, function_obj, $($selectedItem).data('variable_reference'));
  104. break;
  105. }
  106. }
  107. });
  108. }
  109. dom_object.find('.width-dynamic').on('input', function() {
  110. var inputWidth = $(this).textWidth()+10;
  111. $(this).focus();
  112. var tmpStr = $(this).val();
  113. $(this).val('');
  114. $(this).val(tmpStr);
  115. $(this).css({
  116. width: inputWidth
  117. })
  118. }).trigger('input');
  119. }
  120. function openInputToFunction (command, ref_object, dom_object, menu_var_or_value, function_obj, function_selected) {
  121. ref_object.function_called = function_selected;
  122. ref_object.parameters_list = [];
  123. if (function_selected.parameters_list != null && function_selected.parameters_list.length > 0) {
  124. menu_var_or_value.find('.text').text(' ');
  125. dom_object.find('.menu_var_or_value_dom').remove();
  126. var parameters_menu = '<div class="parameters_function_called"> '+function_selected.name+' <span> ( </span>';
  127. for (var j = 0; j < function_selected.parameters_list.length; j++) {
  128. parameters_menu += '<div class="parameter_'+j+'"></div>';
  129. if ((j + 1) != function_selected.parameters_list.length) {
  130. parameters_menu += ' , ';
  131. }
  132. }
  133. parameters_menu += '<span> ) </span></div>';
  134. parameters_menu = $(parameters_menu);
  135. dom_object.append(parameters_menu);
  136. for (var j = 0; j < function_selected.parameters_list.length; j++) {
  137. var temp = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  138. ref_object.parameters_list.push(temp);
  139. renderMenu(command, temp, parameters_menu.find('.parameter_'+j), function_obj);
  140. }
  141. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  142. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  143. context_menu += '</div></div>';
  144. context_menu = $(context_menu);
  145. context_menu.insertAfter( dom_object.find('.parameters_function_called') );
  146. context_menu.dropdown({
  147. onChange: function(value, text, $selectedItem) {
  148. if ($selectedItem.data('clear')) {
  149. dom_object.text('');
  150. ref_object.content = null;
  151. ref_object.row = null;
  152. ref_object.column = null;
  153. delete ref_object.function_called;
  154. delete ref_object.parameters_list;
  155. renderMenu(command, ref_object, dom_object, function_obj);
  156. }
  157. }
  158. });
  159. } else {
  160. menu_var_or_value.find('.text').append('<span> ( ) </span>');
  161. }
  162. if (command.type == Models.COMMAND_TYPES.attribution) {
  163. AttribuitionsManagement.renderMenuOperations(command, ref_object, dom_object, menu_var_or_value, function_obj);
  164. }
  165. if (command.type == Models.COMMAND_TYPES.writer) {
  166. WritersManagement.addContent(command, ref_object, dom_object, menu_var_or_value, function_obj, ref_object.content);
  167. }
  168. }
  169. function openInputToVariable (command, ref_object, dom_object, menu_var_or_value, function_obj, variable_selected) {
  170. ref_object.content = variable_selected;
  171. menu_var_or_value.find('.text').text(' ');
  172. dom_object.find('.menu_var_or_value_dom').remove();
  173. var variable_render = '<div class="variable_rendered"> <span class="var_name">'+variable_selected.name+'</span>';
  174. if (variable_selected.dimensions == 1) {
  175. variable_render += ' <span>[ </span> <div class="column_container"></div> <span> ]</span>';
  176. }
  177. if (variable_selected.dimensions == 2) {
  178. variable_render += ' <span>[ </span> <div class="row_container"></div> <span> ]</span> ';
  179. variable_render += ' <span>[ </span> <div class="column_container"></div> <span> ]</span>';
  180. }
  181. variable_render += '</div>';
  182. variable_render = $(variable_render);
  183. dom_object.append(variable_render);
  184. if (variable_selected.dimensions == 1) {
  185. ref_object.column = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  186. renderMenu(command, ref_object.column, variable_render.find('.column_container'), function_obj);
  187. }
  188. if (variable_selected.dimensions == 2) {
  189. ref_object.row = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  190. renderMenu(command, ref_object.row, variable_render.find('.row_container'), function_obj);
  191. ref_object.column = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  192. renderMenu(command, ref_object.column, variable_render.find('.column_container'), function_obj);
  193. }
  194. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  195. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  196. context_menu += '</div></div>';
  197. context_menu = $(context_menu);
  198. context_menu.insertAfter( dom_object.find('.variable_rendered') );
  199. context_menu.dropdown({
  200. onChange: function(value, text, $selectedItem) {
  201. if ($selectedItem.data('clear')) {
  202. dom_object.text('');
  203. ref_object.content = null;
  204. ref_object.row = null;
  205. ref_object.column = null;
  206. renderMenu(command, ref_object, dom_object, function_obj);
  207. }
  208. }
  209. });
  210. if (command.type == Models.COMMAND_TYPES.attribution) {
  211. AttribuitionsManagement.renderMenuOperations(command, ref_object, dom_object, menu_var_or_value, function_obj, variable_selected);
  212. }
  213. if (command.type == Models.COMMAND_TYPES.writer) {
  214. WritersManagement.addContent(command, ref_object, dom_object, menu_var_or_value, function_obj, variable_selected);
  215. }
  216. }
  217. function openInputToValue (command, ref_object, dom_object, menu_var_or_value, function_obj) {
  218. if (ref_object.content == null) {
  219. ref_object.content = "";
  220. }
  221. menu_var_or_value.find('.text').text(' ');
  222. var field = $('<input type="text" size="2" class="width-dynamic-minus" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />');
  223. field.insertBefore(dom_object.find('.menu_var_or_value_dom'));
  224. var rendered = $('<div class="value_rendered"></div>');
  225. rendered.insertBefore(field);
  226. field.focus();
  227. field.val(ref_object.content);
  228. dom_object.find('.menu_var_or_value_dom').remove();
  229. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  230. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  231. context_menu += '</div></div>';
  232. context_menu = $(context_menu);
  233. context_menu.insertAfter( field );
  234. context_menu.dropdown({
  235. onChange: function(value, text, $selectedItem) {
  236. if ($selectedItem.data('clear')) {
  237. dom_object.text('');
  238. ref_object = new Models.VariableValueMenu(ref_object.variable_and_value, null, null, null, ref_object.include_constant);
  239. dom_object.find('.value_rendered').remove();
  240. dom_object.find('.context_menu_clear').remove();
  241. dom_object.find('.width-dynamic-minus').remove();
  242. renderMenu(command, ref_object, dom_object, function_obj);
  243. }
  244. }
  245. });
  246. dom_object.find('.width-dynamic-minus').focusout(function() {
  247. if ($(this).val().trim()) {
  248. ref_object.content = $(this).val().trim();
  249. }
  250. rendered.text(ref_object.content);
  251. $(this).remove();
  252. });
  253. dom_object.find('.width-dynamic-minus').on('keydown', function(e) {
  254. var code = e.keyCode || e.which;
  255. if(code == 13) {
  256. if ($(this).val().trim()) {
  257. ref_object.content = $(this).val().trim();
  258. }
  259. rendered.text(ref_object.content);
  260. $(this).remove();
  261. }
  262. if(code == 27) {
  263. rendered.text(ref_object.content);
  264. $(this).remove();
  265. }
  266. });
  267. $(rendered).on('click', function(e) {
  268. rendered.remove();
  269. rendered.empty();
  270. rendered.remove();
  271. dom_object.empty();
  272. dom_object.append('<span class="menu_var_or_value_dom"> </span>');
  273. openInputToValue(command, ref_object, dom_object, menu_var_or_value, function_obj)
  274. });
  275. if (command.type == Models.COMMAND_TYPES.attribution) {
  276. AttribuitionsManagement.renderMenuOperations(command, ref_object, dom_object, menu_var_or_value, function_obj);
  277. }
  278. if (command.type == Models.COMMAND_TYPES.writer) {
  279. WritersManagement.addContent(command, ref_object, dom_object, menu_var_or_value, function_obj, ref_object.content);
  280. }
  281. }
  282. $.fn.textWidth = function(text, font) {
  283. if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
  284. $.fn.textWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));
  285. return $.fn.textWidth.fakeEl.width();
  286. };