variable_value_menu.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 WatchJS from 'melanke-watchjs';
  9. var watch = WatchJS.watch;
  10. 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,
  11. value_and_function: 6, all: 7});
  12. export function renderMenu (command, ref_object, dom_object, function_obj, size_field = 2) {
  13. 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">';
  14. if ((ref_object.variable_and_value == VAR_OR_VALUE_TYPES.only_function) || (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.variable_and_function)
  15. || (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.value_and_function) || (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.all)) {
  16. menu_var_or_value += '<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_function+'"><i class="dropdown icon"></i>'+LocalizedStrings.getUI('btn_function')+'</div>';
  17. }
  18. if (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.only_variable) {
  19. 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">';
  20. menu_var_or_value += '</div>';
  21. }
  22. 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)) {
  23. menu_var_or_value += '<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'"><i class="dropdown icon"></i>'+LocalizedStrings.getUI('variable');
  24. menu_var_or_value += '<div class="menu menu_only_vars">';
  25. menu_var_or_value += '</div></div>';
  26. }
  27. if (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.only_value) {
  28. menu_var_or_value = '<input type="text" class="width-dynamic" size="'+size_field+'" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />';
  29. }
  30. if ((ref_object.variable_and_value == VAR_OR_VALUE_TYPES.variable_and_value_opt)
  31. || (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.value_and_function) || (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.all)) {
  32. menu_var_or_value += '<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_value+'">'+LocalizedStrings.getUI('text_value')+'</div>';
  33. }
  34. menu_var_or_value += '</div></div>';
  35. menu_var_or_value = $(menu_var_or_value);
  36. $(dom_object).append(menu_var_or_value);
  37. addHandlers(command, ref_object, dom_object, menu_var_or_value, function_obj);
  38. addVariablesToMenu(function_obj, menu_var_or_value, ref_object);
  39. }
  40. function addVariablesToMenu (function_obj, menu_var_or_value, ref_object) {
  41. watch(window.program_obj.globals, function(){
  42. addVariablesToMenu(function_obj, menu_var_or_value, ref_object);
  43. }, 1);
  44. watch(function_obj.parameters_list, function(){
  45. addVariablesToMenu(function_obj, menu_var_or_value, ref_object);
  46. }, 1);
  47. watch(function_obj.variables_list, function(){
  48. addVariablesToMenu(function_obj, menu_var_or_value, ref_object);
  49. }, 1);
  50. var sub_menu = $(menu_var_or_value).find('.menu_only_vars');
  51. $(sub_menu).text('');
  52. if (window.program_obj.globals) {
  53. if (ref_object.include_constant) {
  54. for (var i = 0; i < window.program_obj.globals.length; i++) {
  55. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + window.program_obj.globals[i].name + ' </div>');
  56. $(temp).data('variable_reference', window.program_obj.globals[i]);
  57. $(sub_menu).append(temp);
  58. }
  59. } else {
  60. for (var i = 0; i < window.program_obj.globals.length; i++) {
  61. if (!window.program_obj.globals[i].is_constant) {
  62. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + window.program_obj.globals[i].name + ' </div>');
  63. $(temp).data('variable_reference', window.program_obj.globals[i]);
  64. $(sub_menu).append(temp);
  65. }
  66. }
  67. }
  68. }
  69. if (function_obj.parameters_list) {
  70. for (var i = 0; i < function_obj.parameters_list.length; i++) {
  71. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + function_obj.parameters_list[i].name + ' </div>');
  72. $(temp).data('variable_reference', function_obj.parameters_list[i]);
  73. $(sub_menu).append(temp);
  74. }
  75. }
  76. if (function_obj.variables_list) {
  77. for (var i = 0; i < function_obj.variables_list.length; i++) {
  78. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + function_obj.variables_list[i].name + ' </div>');
  79. $(temp).data('variable_reference', function_obj.variables_list[i]);
  80. $(sub_menu).append(temp);
  81. }
  82. }
  83. }
  84. function addHandlers (command, ref_object, dom_object, menu_var_or_value, function_obj) {
  85. if (ref_object.variable_and_value != VAR_OR_VALUE_TYPES.only_value) {
  86. $(menu_var_or_value).dropdown({
  87. onChange: function(value, text, $selectedItem) {
  88. $(dom_object).find('.var_name').remove();
  89. switch ($($selectedItem).data('option')) {
  90. case VAR_OR_VALUE_TYPES.only_function:
  91. console.log("foi função");
  92. break;
  93. case VAR_OR_VALUE_TYPES.only_value:
  94. openInputToValue(command, ref_object, dom_object, menu_var_or_value, function_obj);
  95. break;
  96. case VAR_OR_VALUE_TYPES.only_variable:
  97. openInputToVariable(command, ref_object, dom_object, menu_var_or_value, function_obj, $($selectedItem).data('variable_reference'));
  98. break;
  99. }
  100. }
  101. });
  102. }
  103. $(dom_object).find('.width-dynamic').on('input', function() {
  104. var inputWidth = $(this).textWidth()+10;
  105. $(this).focus();
  106. var tmpStr = $(this).val();
  107. $(this).val('');
  108. $(this).val(tmpStr);
  109. $(this).css({
  110. width: inputWidth
  111. })
  112. }).trigger('input');
  113. }
  114. function openInputToVariable (command, ref_object, dom_object, menu_var_or_value, function_obj, variable_selected) {
  115. ref_object.content = variable_selected;
  116. $(menu_var_or_value).find('.text').text(' ');
  117. $(dom_object).find('.menu_var_or_value_dom').remove();
  118. var variable_render = '<div class="variable_rendered"> <span class="var_name">'+variable_selected.name+'</span>';
  119. if (variable_selected.dimensions == 1) {
  120. variable_render += ' <span>[ </span> <div class="column_container"></div> <span> ]</span>';
  121. }
  122. if (variable_selected.dimensions == 2) {
  123. variable_render += ' <span>[ </span> <div class="row_container"></div> <span> ]</span> ';
  124. variable_render += ' <span>[ </span> <div class="column_container"></div> <span> ]</span>';
  125. }
  126. variable_render += '</div>';
  127. variable_render = $(variable_render);
  128. $(dom_object).append(variable_render);
  129. if (variable_selected.dimensions == 1) {
  130. ref_object.column = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  131. renderMenu(command, ref_object.column, $(variable_render).find('.column_container'), function_obj);
  132. }
  133. if (variable_selected.dimensions == 2) {
  134. ref_object.row = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  135. renderMenu(command, ref_object.row, $(variable_render).find('.row_container'), function_obj);
  136. ref_object.column = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  137. renderMenu(command, ref_object.column, $(variable_render).find('.column_container'), function_obj);
  138. }
  139. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  140. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  141. context_menu += '</div></div>';
  142. context_menu = $(context_menu);
  143. $( context_menu ).insertAfter( $(dom_object).find('.variable_rendered') );
  144. $(context_menu).dropdown({
  145. onChange: function(value, text, $selectedItem) {
  146. if ($($selectedItem).data('clear')) {
  147. $(dom_object).text('');
  148. ref_object = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  149. renderMenu(command, ref_object, dom_object, function_obj);
  150. }
  151. }
  152. });
  153. if (command.type == Models.COMMAND_TYPES.attribution) {
  154. AttribuitionsManagement.renderMenuOperations(command, ref_object, dom_object, menu_var_or_value, function_obj, variable_selected);
  155. }
  156. }
  157. function openInputToValue (command, ref_object, dom_object, menu_var_or_value, function_obj) {
  158. if (ref_object.content == null) {
  159. ref_object.content = "";
  160. }
  161. $(menu_var_or_value).find('.text').text(' ');
  162. var field = $('<input type="text" size="2" class="width-dynamic-minus" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />');
  163. $( field ).insertBefore($(dom_object).find('.menu_var_or_value_dom'));
  164. var rendered = $('<div class="value_rendered"></div>');
  165. $( rendered ).insertBefore(field);
  166. field.focus();
  167. $(field).val(ref_object.content);
  168. $(dom_object).find('.menu_var_or_value_dom').remove();
  169. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  170. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  171. context_menu += '</div></div>';
  172. context_menu = $(context_menu);
  173. $( context_menu ).insertAfter( field );
  174. $(context_menu).dropdown({
  175. onChange: function(value, text, $selectedItem) {
  176. if ($($selectedItem).data('clear')) {
  177. $(dom_object).text('');
  178. ref_object = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  179. $(dom_object).find('.value_rendered').remove();
  180. $(dom_object).find('.context_menu_clear').remove();
  181. $(dom_object).find('.width-dynamic-minus').remove();
  182. renderMenu(command, ref_object, dom_object, function_obj);
  183. }
  184. }
  185. });
  186. $(dom_object).find('.width-dynamic-minus').focusout(function() {
  187. if ($(this).val().trim()) {
  188. ref_object.content = $(this).val().trim();
  189. }
  190. $(rendered).text(ref_object.content);
  191. $(this).remove();
  192. });
  193. $(dom_object).find('.width-dynamic-minus').on('keydown', function(e) {
  194. var code = e.keyCode || e.which;
  195. if(code == 13) {
  196. if ($(this).val().trim()) {
  197. ref_object.content = $(this).val().trim();
  198. }
  199. $(rendered).text(ref_object.content);
  200. $(this).remove();
  201. }
  202. if(code == 27) {
  203. $(rendered).text(ref_object.content);
  204. $(this).remove();
  205. }
  206. });
  207. $(rendered).on('click', function(e) {
  208. console.log('no clique, vou passar o seguinte: ');
  209. console.log(dom_object);
  210. rendered.remove();
  211. rendered.empty();
  212. $(rendered).remove();
  213. $(dom_object).empty();
  214. $(dom_object).append('<span class="menu_var_or_value_dom"> </span>');
  215. //$('<span class="menu_var_or_value_dom"> </span>').insertBefore($(dom_object).find('.value_rendered'));
  216. //$(dom_object).find('.value_rendered').remove();
  217. //$(dom_object).find('.context_menu_clear').remove();
  218. //$(dom_object).find('.width-dynamic-minus').remove();
  219. openInputToValue(command, ref_object, dom_object, menu_var_or_value, function_obj)
  220. });
  221. if (command.type == Models.COMMAND_TYPES.attribution) {
  222. AttribuitionsManagement.renderMenuOperations(command, ref_object, dom_object, menu_var_or_value, function_obj);
  223. }
  224. }
  225. $.fn.textWidth = function(text, font) {
  226. if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
  227. $.fn.textWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));
  228. return $.fn.textWidth.fakeEl.width();
  229. };