variable_value_menu.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. if (ref_object.content) {
  46. renderPreviousContent(function_obj, menu_var_or_value, ref_object, dom_object, command);
  47. }
  48. }
  49. function renderPreviousContent (function_obj, menu_var_or_value, ref_object, dom_object, command) {
  50. if (ref_object.function_called) {
  51. } else if (ref_object.content.type) {
  52. menu_var_or_value.remove();
  53. variableValueMenuCode(command, ref_object, dom_object, function_obj);
  54. } else {
  55. var temp = $('<div class="value_rendered"></div>');
  56. temp.insertBefore(menu_var_or_value);
  57. menu_var_or_value.remove();
  58. variableValueMenuCode(command, ref_object, temp, function_obj);
  59. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  60. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  61. context_menu += '</div></div>';
  62. context_menu = $(context_menu);
  63. context_menu.insertAfter( temp );
  64. context_menu.dropdown({
  65. onChange: function(value, text, $selectedItem) {
  66. if ($selectedItem.data('clear')) {
  67. dom_object.text('');
  68. ref_object = new Models.VariableValueMenu(ref_object.variable_and_value, null, null, null, ref_object.include_constant);
  69. dom_object.find('.value_rendered').remove();
  70. dom_object.find('.context_menu_clear').remove();
  71. dom_object.find('.width-dynamic-minus').remove();
  72. renderMenu(command, ref_object, dom_object, function_obj);
  73. }
  74. }
  75. });
  76. temp.on('click', function(e) {
  77. temp.remove();
  78. temp.empty();
  79. temp.remove();
  80. dom_object.empty();
  81. dom_object.append('<span class="menu_var_or_value_dom"> </span>');
  82. openInputToValue(command, ref_object, dom_object, menu_var_or_value, function_obj);
  83. });
  84. }
  85. }
  86. function variableValueMenuCode (command, variable_obj, dom_object, function_obj) {
  87. var ret = '';
  88. if (variable_obj.function_called) {
  89. ret += variable_obj.function_called.name + ' ( ';
  90. if (variable_obj.parameters_list) {
  91. for (var i = 0; i < variable_obj.parameters_list.length; i++) {
  92. variableValueMenuCode(command, variable_obj.parameters_list[i], dom_object, function_obj);
  93. if ((i + 1) < variable_obj.parameters_list.length) {
  94. ret += ', ';
  95. }
  96. }
  97. }
  98. ret += ' )';
  99. dom_object.append($(ret));
  100. } else if (variable_obj.content.type) {
  101. var variable_render = "";
  102. if (variable_obj.content.dimensions == 1) {
  103. variable_render = '<div class="variable_rendered"> <span class="var_name">'+variable_obj.content.name+'</span>';
  104. variable_render += ' <span>[ </span> <div class="column_container"></div> <span> ]</span>';
  105. variable_render += '</div>';
  106. variable_render = $(variable_render);
  107. dom_object.append(variable_render);
  108. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  109. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  110. context_menu += '</div></div>';
  111. context_menu = $(context_menu);
  112. //context_menu.insertAfter( dom_object.find('.variable_rendered') );
  113. variable_render.append(context_menu);
  114. context_menu.dropdown({
  115. onChange: function(value, text, $selectedItem) {
  116. if ($selectedItem.data('clear')) {
  117. dom_object.text('');
  118. variable_obj.content = null;
  119. variable_obj.row = null;
  120. variable_obj.column = null;
  121. renderMenu(command, variable_obj, dom_object, function_obj);
  122. }
  123. }
  124. });
  125. variableValueMenuCode(command, variable_obj.column, $(variable_render.find('.column_container')[0]), function_obj);
  126. } else if (variable_obj.content.dimensions == 2) {
  127. /*ret += ' [ ' + variableValueMenuCode(command, variable_obj.row, dom_object, function_obj) + ' ] ';
  128. ret += ' [ ' + variableValueMenuCode(command, variable_obj.column, dom_object, function_obj) + ' ] ';*/
  129. } else {
  130. variable_render = '<div class="variable_rendered"> <span class="var_name">'+variable_obj.content.name+'</span>';
  131. variable_render += '</div>';
  132. variable_render = $(variable_render);
  133. dom_object.append(variable_render);
  134. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  135. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  136. context_menu += '</div></div>';
  137. context_menu = $(context_menu);
  138. context_menu.insertAfter( variable_render );
  139. context_menu.dropdown({
  140. onChange: function(value, text, $selectedItem) {
  141. if ($selectedItem.data('clear')) {
  142. dom_object.text('');
  143. variable_obj.content = null;
  144. variable_obj.row = null;
  145. variable_obj.column = null;
  146. renderMenu(command, variable_obj, dom_object, function_obj);
  147. }
  148. }
  149. });
  150. }
  151. } else {
  152. console.log("numeral?");
  153. var variable_render = '<div class="variable_rendered"> <span class="var_name">'+variable_obj.content+'</span>';
  154. variable_render += '</div>';
  155. console.log(ret);
  156. dom_object.append($(variable_render));
  157. }
  158. }
  159. function addFunctionsToMenu (function_obj, menu_var_or_value, ref_object) {
  160. var sub_menu = menu_var_or_value.find('.menu_only_functions');
  161. sub_menu.text('');
  162. for (var i = 0; i < window.program_obj.functions.length; i++) {
  163. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_function+'">' + window.program_obj.functions[i].name + ' </div>');
  164. temp.data('function_reference', window.program_obj.functions[i]);
  165. sub_menu.append(temp);
  166. }
  167. }
  168. function addVariablesToMenu (function_obj, menu_var_or_value, ref_object) {
  169. var sub_menu = menu_var_or_value.find('.menu_only_vars');
  170. sub_menu.text('');
  171. if (window.program_obj.globals) {
  172. if (ref_object.include_constant) {
  173. for (var i = 0; i < window.program_obj.globals.length; i++) {
  174. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + window.program_obj.globals[i].name + ' </div>');
  175. temp.data('variable_reference', window.program_obj.globals[i]);
  176. sub_menu.append(temp);
  177. }
  178. } else {
  179. for (var i = 0; i < window.program_obj.globals.length; i++) {
  180. if (!window.program_obj.globals[i].is_constant) {
  181. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + window.program_obj.globals[i].name + ' </div>');
  182. temp.data('variable_reference', window.program_obj.globals[i]);
  183. sub_menu.append(temp);
  184. }
  185. }
  186. }
  187. }
  188. if (function_obj.parameters_list) {
  189. for (var i = 0; i < function_obj.parameters_list.length; i++) {
  190. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + function_obj.parameters_list[i].name + ' </div>');
  191. temp.data('variable_reference', function_obj.parameters_list[i]);
  192. sub_menu.append(temp);
  193. }
  194. }
  195. if (function_obj.variables_list) {
  196. for (var i = 0; i < function_obj.variables_list.length; i++) {
  197. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + function_obj.variables_list[i].name + ' </div>');
  198. temp.data('variable_reference', function_obj.variables_list[i]);
  199. sub_menu.append(temp);
  200. }
  201. }
  202. }
  203. function addHandlers (command, ref_object, dom_object, menu_var_or_value, function_obj) {
  204. if (ref_object.variable_and_value != VAR_OR_VALUE_TYPES.only_value) {
  205. menu_var_or_value.dropdown({
  206. onChange: function(value, text, $selectedItem) {
  207. dom_object.find('.var_name').remove();
  208. switch ($selectedItem.data('option')) {
  209. case VAR_OR_VALUE_TYPES.only_function:
  210. openInputToFunction(command, ref_object, dom_object, menu_var_or_value, function_obj, $($selectedItem).data('function_reference'));
  211. break;
  212. case VAR_OR_VALUE_TYPES.only_value:
  213. openInputToValue(command, ref_object, dom_object, menu_var_or_value, function_obj);
  214. break;
  215. case VAR_OR_VALUE_TYPES.only_variable:
  216. openInputToVariable(command, ref_object, dom_object, menu_var_or_value, function_obj, $($selectedItem).data('variable_reference'));
  217. break;
  218. }
  219. }
  220. });
  221. }
  222. dom_object.find('.width-dynamic').on('input', function() {
  223. var inputWidth = $(this).textWidth()+10;
  224. $(this).focus();
  225. var tmpStr = $(this).val();
  226. $(this).val('');
  227. $(this).val(tmpStr);
  228. $(this).css({
  229. width: inputWidth
  230. })
  231. }).trigger('input');
  232. }
  233. function openInputToFunction (command, ref_object, dom_object, menu_var_or_value, function_obj, function_selected) {
  234. ref_object.function_called = function_selected;
  235. ref_object.parameters_list = [];
  236. if (function_selected.parameters_list != null && function_selected.parameters_list.length > 0) {
  237. menu_var_or_value.find('.text').text(' ');
  238. dom_object.find('.menu_var_or_value_dom').remove();
  239. var parameters_menu = '<div class="parameters_function_called"> '+function_selected.name+' <span> ( </span>';
  240. for (var j = 0; j < function_selected.parameters_list.length; j++) {
  241. parameters_menu += '<div class="parameter_'+j+'"></div>';
  242. if ((j + 1) != function_selected.parameters_list.length) {
  243. parameters_menu += ' , ';
  244. }
  245. }
  246. parameters_menu += '<span> ) </span></div>';
  247. parameters_menu = $(parameters_menu);
  248. dom_object.append(parameters_menu);
  249. for (var j = 0; j < function_selected.parameters_list.length; j++) {
  250. var temp = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  251. ref_object.parameters_list.push(temp);
  252. renderMenu(command, temp, parameters_menu.find('.parameter_'+j), function_obj);
  253. }
  254. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  255. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  256. context_menu += '</div></div>';
  257. context_menu = $(context_menu);
  258. context_menu.insertAfter( dom_object.find('.parameters_function_called') );
  259. context_menu.dropdown({
  260. onChange: function(value, text, $selectedItem) {
  261. if ($selectedItem.data('clear')) {
  262. dom_object.text('');
  263. ref_object.content = null;
  264. ref_object.row = null;
  265. ref_object.column = null;
  266. delete ref_object.function_called;
  267. delete ref_object.parameters_list;
  268. renderMenu(command, ref_object, dom_object, function_obj);
  269. }
  270. }
  271. });
  272. } else {
  273. menu_var_or_value.find('.text').append('<span> ( ) </span>');
  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. function openInputToVariable (command, ref_object, dom_object, menu_var_or_value, function_obj, variable_selected) {
  283. ref_object.content = variable_selected;
  284. menu_var_or_value.find('.text').text(' ');
  285. dom_object.find('.menu_var_or_value_dom').remove();
  286. var variable_render = '<div class="variable_rendered"> <span class="var_name">'+variable_selected.name+'</span>';
  287. if (variable_selected.dimensions == 1) {
  288. variable_render += ' <span>[ </span> <div class="column_container"></div> <span> ]</span>';
  289. }
  290. if (variable_selected.dimensions == 2) {
  291. variable_render += ' <span>[ </span> <div class="row_container"></div> <span> ]</span> ';
  292. variable_render += ' <span>[ </span> <div class="column_container"></div> <span> ]</span>';
  293. }
  294. variable_render += '</div>';
  295. variable_render = $(variable_render);
  296. dom_object.append(variable_render);
  297. if (variable_selected.dimensions == 1) {
  298. ref_object.column = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  299. renderMenu(command, ref_object.column, variable_render.find('.column_container'), function_obj);
  300. }
  301. if (variable_selected.dimensions == 2) {
  302. ref_object.row = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  303. renderMenu(command, ref_object.row, variable_render.find('.row_container'), function_obj);
  304. ref_object.column = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  305. renderMenu(command, ref_object.column, variable_render.find('.column_container'), function_obj);
  306. }
  307. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  308. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  309. context_menu += '</div></div>';
  310. context_menu = $(context_menu);
  311. context_menu.insertAfter( dom_object.find('.variable_rendered') );
  312. context_menu.dropdown({
  313. onChange: function(value, text, $selectedItem) {
  314. if ($selectedItem.data('clear')) {
  315. dom_object.text('');
  316. ref_object.content = null;
  317. ref_object.row = null;
  318. ref_object.column = null;
  319. renderMenu(command, ref_object, dom_object, function_obj);
  320. }
  321. }
  322. });
  323. if (command.type == Models.COMMAND_TYPES.attribution) {
  324. AttribuitionsManagement.renderMenuOperations(command, ref_object, dom_object, menu_var_or_value, function_obj, variable_selected);
  325. }
  326. if (command.type == Models.COMMAND_TYPES.writer) {
  327. WritersManagement.addContent(command, ref_object, dom_object, menu_var_or_value, function_obj, variable_selected);
  328. }
  329. }
  330. function openInputToValue (command, ref_object, dom_object, menu_var_or_value, function_obj) {
  331. if (ref_object.content == null) {
  332. ref_object.content = "";
  333. }
  334. menu_var_or_value.find('.text').text(' ');
  335. var field = $('<input type="text" size="2" class="width-dynamic-minus" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />');
  336. field.insertBefore(dom_object.find('.menu_var_or_value_dom'));
  337. var rendered = $('<div class="value_rendered"></div>');
  338. rendered.insertBefore(field);
  339. field.focus();
  340. field.val(ref_object.content);
  341. dom_object.find('.menu_var_or_value_dom').remove();
  342. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  343. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  344. context_menu += '</div></div>';
  345. context_menu = $(context_menu);
  346. context_menu.insertAfter( field );
  347. context_menu.dropdown({
  348. onChange: function(value, text, $selectedItem) {
  349. if ($selectedItem.data('clear')) {
  350. dom_object.text('');
  351. ref_object = new Models.VariableValueMenu(ref_object.variable_and_value, null, null, null, ref_object.include_constant);
  352. dom_object.find('.value_rendered').remove();
  353. dom_object.find('.context_menu_clear').remove();
  354. dom_object.find('.width-dynamic-minus').remove();
  355. renderMenu(command, ref_object, dom_object, function_obj);
  356. }
  357. }
  358. });
  359. dom_object.find('.width-dynamic-minus').focusout(function() {
  360. if ($(this).val().trim()) {
  361. ref_object.content = $(this).val().trim();
  362. }
  363. rendered.text(ref_object.content);
  364. $(this).remove();
  365. });
  366. dom_object.find('.width-dynamic-minus').on('keydown', function(e) {
  367. var code = e.keyCode || e.which;
  368. if(code == 13) {
  369. if ($(this).val().trim()) {
  370. ref_object.content = $(this).val().trim();
  371. }
  372. rendered.text(ref_object.content);
  373. $(this).remove();
  374. }
  375. if(code == 27) {
  376. rendered.text(ref_object.content);
  377. $(this).remove();
  378. }
  379. });
  380. rendered.on('click', function(e) {
  381. rendered.remove();
  382. rendered.empty();
  383. rendered.remove();
  384. dom_object.empty();
  385. dom_object.append('<span class="menu_var_or_value_dom"> </span>');
  386. openInputToValue(command, ref_object, dom_object, menu_var_or_value, function_obj)
  387. });
  388. if (command.type == Models.COMMAND_TYPES.attribution) {
  389. AttribuitionsManagement.renderMenuOperations(command, ref_object, dom_object, menu_var_or_value, function_obj);
  390. }
  391. if (command.type == Models.COMMAND_TYPES.writer) {
  392. WritersManagement.addContent(command, ref_object, dom_object, menu_var_or_value, function_obj, ref_object.content);
  393. }
  394. }
  395. $.fn.textWidth = function(text, font) {
  396. if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
  397. $.fn.textWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));
  398. return $.fn.textWidth.fakeEl.width();
  399. };