variable_value_menu.js 12 KB

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