variable_value_menu.js 18 KB

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