variable_value_menu.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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 * as RepeatNTimesManagement from './repeatNtimes';
  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, expression_element) {
  13. var menu_var_or_value = '<div class="ui dropdown menu_var_or_value_dom" data-algo="12"><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) {
  15. 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">';
  16. menu_var_or_value += '</div>';
  17. }
  18. if ((ref_object.variable_and_value == VAR_OR_VALUE_TYPES.variable_and_function)
  19. || (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.value_and_function) || (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.all)) {
  20. menu_var_or_value += '<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_function+'"><i class="dropdown icon"></i>'+LocalizedStrings.getUI('btn_function');
  21. menu_var_or_value += '<div class="menu menu_only_functions">';
  22. menu_var_or_value += '</div></div>';
  23. }
  24. if (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.only_variable) {
  25. 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">';
  26. menu_var_or_value += '</div>';
  27. }
  28. 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)) {
  29. menu_var_or_value += '<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'"><i class="dropdown icon"></i>'+LocalizedStrings.getUI('variable');
  30. menu_var_or_value += '<div class="menu menu_only_vars">';
  31. menu_var_or_value += '</div></div>';
  32. }
  33. if (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.only_value) {
  34. menu_var_or_value = '<input type="text" class="width-dynamic" size="'+size_field+'" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />';
  35. }
  36. if ((ref_object.variable_and_value == VAR_OR_VALUE_TYPES.variable_and_value_opt)
  37. || (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.value_and_function) || (ref_object.variable_and_value == VAR_OR_VALUE_TYPES.all)) {
  38. menu_var_or_value += '<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_value+'">'+LocalizedStrings.getUI('text_value')+'</div>';
  39. if (command.type == Models.COMMAND_TYPES.attribution) {
  40. menu_var_or_value += '<div class="item"><i class="dropdown icon"></i>' + LocalizedStrings.getUI('text_change');
  41. menu_var_or_value += '<div class="menu">';
  42. menu_var_or_value += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.exp_op_exp+'">EXP OP EXP</div>';
  43. menu_var_or_value += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.op_exp+'">OP EXP</div>';
  44. menu_var_or_value += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.par_exp_par+'">( EXP )</div>';
  45. menu_var_or_value += '</div></div>';
  46. }
  47. }
  48. menu_var_or_value += '</div></div>';
  49. menu_var_or_value = $(menu_var_or_value);
  50. dom_object.append(menu_var_or_value);
  51. addHandlers(command, ref_object, dom_object, menu_var_or_value, function_obj, expression_element);
  52. addVariablesToMenu(function_obj, menu_var_or_value, ref_object, expression_element);
  53. addFunctionsToMenu(function_obj, menu_var_or_value, ref_object, expression_element);
  54. if (ref_object.content || ref_object.function_called) {
  55. renderPreviousContent(function_obj, menu_var_or_value, ref_object, dom_object, command, expression_element);
  56. }
  57. }
  58. export function refreshMenu (menu_var_or_value_dom) {
  59. console.log('\n\n');
  60. console.log(menu_var_or_value_dom);
  61. console.log("olá, fui chamado! note alguns DATAS recuperados: ");
  62. console.log(menu_var_or_value_dom.data());
  63. console.log('\n\n\n');
  64. }
  65. function renderPreviousContent (function_obj, menu_var_or_value, ref_object, dom_object, command, expression_element) {
  66. if (ref_object.function_called) {
  67. menu_var_or_value.remove();
  68. variableValueMenuCode(command, ref_object, dom_object, function_obj, menu_var_or_value, expression_element);
  69. } else if (ref_object.content.type) {
  70. menu_var_or_value.remove();
  71. variableValueMenuCode(command, ref_object, dom_object, function_obj, menu_var_or_value, expression_element);
  72. } else {
  73. menu_var_or_value.remove();
  74. variableValueMenuCode(command, ref_object, dom_object, function_obj, menu_var_or_value, expression_element);
  75. }
  76. }
  77. function variableValueMenuCode (command, variable_obj, dom_object, function_obj, menu_var_or_value, expression_element) {
  78. if (variable_obj.content == null && variable_obj.function_called == null) {
  79. renderMenu(command, variable_obj, dom_object, function_obj, 2, expression_element);
  80. return;
  81. }
  82. var ret = '';
  83. if (variable_obj.function_called) {
  84. if (variable_obj.function_called.parameters_list == null || variable_obj.function_called.length == 0) {
  85. menu_var_or_value.find('.text').text(' ');
  86. dom_object.find('.menu_var_or_value_dom').remove();
  87. var parameters_menu = '<div class="parameters_function_called"> '+variable_obj.function_called.name+' <span> ( </span>';
  88. parameters_menu += '<span> ) </span></div>';
  89. parameters_menu = $(parameters_menu);
  90. dom_object.append(parameters_menu);
  91. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  92. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  93. if (command.type == Models.COMMAND_TYPES.attribution) {
  94. context_menu += '<div class="item"><i class="dropdown icon"></i>' + LocalizedStrings.getUI('text_change');
  95. context_menu += '<div class="menu">';
  96. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.exp_op_exp+'">EXP OP EXP</div>';
  97. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.op_exp+'">OP EXP</div>';
  98. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.par_exp_par+'">( EXP )</div>';
  99. context_menu += '</div></div>';
  100. }
  101. context_menu += '</div></div>';
  102. context_menu = $(context_menu);
  103. context_menu.insertAfter( dom_object.find('.parameters_function_called') );
  104. context_menu.dropdown({
  105. onChange: function(value, text, $selectedItem) {
  106. if ($selectedItem.data('clear')) {
  107. console.log('PP1');
  108. dom_object.text('');
  109. variable_obj.content = null;
  110. variable_obj.row = null;
  111. variable_obj.column = null;
  112. delete variable_obj.function_called;
  113. delete variable_obj.parameters_list;
  114. renderMenu(command, variable_obj, dom_object, function_obj, 2, expression_element);
  115. }
  116. if ($selectedItem.data('exp')) {
  117. AttribuitionsManagement.manageExpressionElements(command, variable_obj, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element);
  118. }
  119. }
  120. });
  121. } else {
  122. menu_var_or_value.find('.text').text(' ');
  123. dom_object.find('.menu_var_or_value_dom').remove();
  124. var parameters_menu = '<div class="parameters_function_called"> '+variable_obj.function_called.name+' <span> ( </span>';
  125. for (var j = 0; j < variable_obj.function_called.parameters_list.length; j++) {
  126. parameters_menu += '<div class="parameter_'+j+'"></div>';
  127. if ((j + 1) != variable_obj.function_called.parameters_list.length) {
  128. parameters_menu += ' , ';
  129. }
  130. }
  131. parameters_menu += '<span> ) </span></div>';
  132. parameters_menu = $(parameters_menu);
  133. dom_object.append(parameters_menu);
  134. for (var j = 0; j < variable_obj.function_called.parameters_list.length; j++) {
  135. renderMenu(command, variable_obj.parameters_list[j], parameters_menu.find('.parameter_'+j), function_obj, 2, expression_element);
  136. }
  137. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  138. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  139. if (command.type == Models.COMMAND_TYPES.attribution) {
  140. context_menu += '<div class="item"><i class="dropdown icon"></i>' + LocalizedStrings.getUI('text_change');
  141. context_menu += '<div class="menu">';
  142. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.exp_op_exp+'">EXP OP EXP</div>';
  143. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.op_exp+'">OP EXP</div>';
  144. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.par_exp_par+'">( EXP )</div>';
  145. context_menu += '</div></div>';
  146. }
  147. context_menu += '</div></div>';
  148. context_menu = $(context_menu);
  149. context_menu.insertAfter( parameters_menu );
  150. context_menu.dropdown({
  151. onChange: function(value, text, $selectedItem) {
  152. if ($selectedItem.data('clear')) {
  153. console.log('PP2');
  154. dom_object.text('');
  155. variable_obj.content = null;
  156. variable_obj.row = null;
  157. variable_obj.column = null;
  158. delete variable_obj.function_called;
  159. delete variable_obj.parameters_list;
  160. renderMenu(command, variable_obj, dom_object, function_obj, 2, expression_element);
  161. }
  162. if ($selectedItem.data('exp')) {
  163. AttribuitionsManagement.manageExpressionElements(command, variable_obj, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element);
  164. }
  165. }
  166. });
  167. }
  168. } else if (variable_obj.content.type) {
  169. var variable_render = "";
  170. if (variable_obj.content.dimensions == 1) {
  171. variable_render = '<div class="variable_rendered"> <span class="var_name">'+variable_obj.content.name+'</span>';
  172. variable_render += ' <span>[ </span> <div class="column_container"></div> <span> ]</span>';
  173. variable_render += '</div>';
  174. variable_render = $(variable_render);
  175. dom_object.append(variable_render);
  176. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  177. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  178. if (command.type == Models.COMMAND_TYPES.attribution) {
  179. context_menu += '<div class="item"><i class="dropdown icon"></i>' + LocalizedStrings.getUI('text_change');
  180. context_menu += '<div class="menu">';
  181. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.exp_op_exp+'">EXP OP EXP</div>';
  182. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.op_exp+'">OP EXP</div>';
  183. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.par_exp_par+'">( EXP )</div>';
  184. context_menu += '</div></div>';
  185. }
  186. context_menu += '</div></div>';
  187. context_menu = $(context_menu);
  188. variable_render.append(context_menu);
  189. context_menu.dropdown({
  190. onChange: function(value, text, $selectedItem) {
  191. if ($selectedItem.data('clear')) {
  192. console.log('PP3');
  193. dom_object.text('');
  194. variable_obj.content = null;
  195. variable_obj.row = null;
  196. variable_obj.column = null;
  197. delete variable_obj.function_called;
  198. delete variable_obj.parameters_list;
  199. renderMenu(command, variable_obj, dom_object, function_obj, 2, expression_element);
  200. }
  201. if ($selectedItem.data('exp')) {
  202. AttribuitionsManagement.manageExpressionElements(command, variable_obj, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element);
  203. }
  204. }
  205. });
  206. variableValueMenuCode(command, variable_obj.column, $(variable_render.find('.column_container')), function_obj, menu_var_or_value, expression_element);
  207. } else if (variable_obj.content.dimensions == 2) {
  208. variable_render = '<div class="variable_rendered"> <span class="var_name">'+variable_obj.content.name+'</span>';
  209. variable_render += ' <span>[ </span> <div class="row_container"></div> <span> ]</span>';
  210. variable_render += ' <span>[ </span> <div class="column_container"></div> <span> ] </span>';
  211. variable_render += '</div>';
  212. variable_render = $(variable_render);
  213. dom_object.append(variable_render);
  214. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  215. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  216. if (command.type == Models.COMMAND_TYPES.attribution) {
  217. context_menu += '<div class="item"><i class="dropdown icon"></i>' + LocalizedStrings.getUI('text_change');
  218. context_menu += '<div class="menu">';
  219. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.exp_op_exp+'">EXP OP EXP</div>';
  220. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.op_exp+'">OP EXP</div>';
  221. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.par_exp_par+'">( EXP )</div>';
  222. context_menu += '</div></div>';
  223. }
  224. context_menu += '</div></div>';
  225. context_menu = $(context_menu);
  226. variable_render.append(context_menu);
  227. context_menu.dropdown({
  228. onChange: function(value, text, $selectedItem) {
  229. if ($selectedItem.data('clear')) {
  230. console.log('PP4');
  231. dom_object.text('');
  232. variable_obj.content = null;
  233. variable_obj.row = null;
  234. variable_obj.column = null;
  235. delete variable_obj.function_called;
  236. delete variable_obj.parameters_list;
  237. renderMenu(command, variable_obj, dom_object, function_obj, 2, expression_element);
  238. }
  239. if ($selectedItem.data('exp')) {
  240. AttribuitionsManagement.manageExpressionElements(command, variable_obj, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element);
  241. }
  242. }
  243. });
  244. variableValueMenuCode(command, variable_obj.row, $(variable_render.find('.row_container')), function_obj, menu_var_or_value, expression_element);
  245. variableValueMenuCode(command, variable_obj.column, $(variable_render.find('.column_container')), function_obj, menu_var_or_value, expression_element);
  246. } else {
  247. variable_render = '<div class="variable_rendered"> <span class="var_name">'+variable_obj.content.name+'</span>';
  248. variable_render += '</div>';
  249. variable_render = $(variable_render);
  250. dom_object.append(variable_render);
  251. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  252. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  253. if (command.type == Models.COMMAND_TYPES.attribution) {
  254. context_menu += '<div class="item"><i class="dropdown icon"></i>' + LocalizedStrings.getUI('text_change');
  255. context_menu += '<div class="menu">';
  256. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.exp_op_exp+'">EXP OP EXP</div>';
  257. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.op_exp+'">OP EXP</div>';
  258. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.par_exp_par+'">( EXP )</div>';
  259. context_menu += '</div></div>';
  260. }
  261. context_menu += '</div></div>';
  262. context_menu = $(context_menu);
  263. variable_render.append(context_menu);
  264. context_menu.dropdown({
  265. onChange: function(value, text, $selectedItem) {
  266. if ($selectedItem.data('clear')) {
  267. console.log('PP5');
  268. dom_object.text('');
  269. variable_obj.content = null;
  270. variable_obj.row = null;
  271. variable_obj.column = null;
  272. delete variable_obj.function_called;
  273. delete variable_obj.parameters_list;
  274. renderMenu(command, variable_obj, dom_object, function_obj, 2, expression_element);
  275. }
  276. if ($selectedItem.data('exp')) {
  277. AttribuitionsManagement.manageExpressionElements(command, variable_obj, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element);
  278. }
  279. }
  280. });
  281. }
  282. } else {
  283. var variable_render = '<div class="variable_rendered"> <span class="var_name">'+variable_obj.content+'</span>';
  284. variable_render += '</div>';
  285. variable_render = $(variable_render);
  286. dom_object.append(variable_render);
  287. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  288. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  289. if (command.type == Models.COMMAND_TYPES.attribution) {
  290. context_menu += '<div class="item"><i class="dropdown icon"></i>' + LocalizedStrings.getUI('text_change');
  291. context_menu += '<div class="menu">';
  292. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.exp_op_exp+'">EXP OP EXP</div>';
  293. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.op_exp+'">OP EXP</div>';
  294. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.par_exp_par+'">( EXP )</div>';
  295. context_menu += '</div></div>';
  296. }
  297. context_menu += '</div></div>';
  298. context_menu = $(context_menu);
  299. if (variable_obj.variable_and_value != VAR_OR_VALUE_TYPES.only_value) {
  300. context_menu.insertAfter( variable_render );
  301. }
  302. context_menu.dropdown({
  303. onChange: function(value, text, $selectedItem) {
  304. if ($selectedItem.data('clear')) {
  305. console.log('PP6');
  306. dom_object.text('');
  307. variable_obj.content = null;
  308. variable_obj.row = null;
  309. variable_obj.column = null;
  310. delete variable_obj.function_called;
  311. delete variable_obj.parameters_list;
  312. dom_object.find('.value_rendered').remove();
  313. dom_object.find('.context_menu_clear').remove();
  314. dom_object.find('.width-dynamic-minus').remove();
  315. renderMenu(command, variable_obj, dom_object, function_obj, 2, expression_element);
  316. }
  317. if ($selectedItem.data('exp')) {
  318. AttribuitionsManagement.manageExpressionElements(command, variable_obj, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element);
  319. }
  320. }
  321. });
  322. variable_render.on('click', function(e) {
  323. variable_render.remove();
  324. variable_render.empty();
  325. variable_render.remove();
  326. dom_object.empty();
  327. dom_object.append('<span class="menu_var_or_value_dom"> </span>');
  328. openInputToValue(command, variable_obj, dom_object, menu_var_or_value, function_obj, expression_element);
  329. });
  330. }
  331. }
  332. function addFunctionsToMenu (function_obj, menu_var_or_value, ref_object, expression_element) {
  333. var sub_menu = menu_var_or_value.find('.menu_only_functions');
  334. sub_menu.text('');
  335. for (var i = 0; i < window.program_obj.functions.length; i++) {
  336. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_function+'">' + window.program_obj.functions[i].name + ' </div>');
  337. temp.data('function_reference', window.program_obj.functions[i]);
  338. sub_menu.append(temp);
  339. }
  340. }
  341. function addVariablesToMenu (function_obj, menu_var_or_value, ref_object, expression_element) {
  342. var sub_menu = menu_var_or_value.find('.menu_only_vars');
  343. sub_menu.text('');
  344. if (window.program_obj.globals) {
  345. if (ref_object.include_constant) {
  346. for (var i = 0; i < window.program_obj.globals.length; i++) {
  347. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + window.program_obj.globals[i].name + ' </div>');
  348. temp.data('variable_reference', window.program_obj.globals[i]);
  349. sub_menu.append(temp);
  350. }
  351. } else {
  352. for (var i = 0; i < window.program_obj.globals.length; i++) {
  353. if (!window.program_obj.globals[i].is_constant) {
  354. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + window.program_obj.globals[i].name + ' </div>');
  355. temp.data('variable_reference', window.program_obj.globals[i]);
  356. sub_menu.append(temp);
  357. }
  358. }
  359. }
  360. }
  361. if (function_obj.parameters_list) {
  362. for (var i = 0; i < function_obj.parameters_list.length; i++) {
  363. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + function_obj.parameters_list[i].name + ' </div>');
  364. temp.data('variable_reference', function_obj.parameters_list[i]);
  365. sub_menu.append(temp);
  366. }
  367. }
  368. if (function_obj.variables_list) {
  369. for (var i = 0; i < function_obj.variables_list.length; i++) {
  370. var temp = $('<div class="item" data-option="'+VAR_OR_VALUE_TYPES.only_variable+'">' + function_obj.variables_list[i].name + ' </div>');
  371. temp.data('variable_reference', function_obj.variables_list[i]);
  372. sub_menu.append(temp);
  373. }
  374. }
  375. }
  376. function addHandlers (command, ref_object, dom_object, menu_var_or_value, function_obj, expression_element) {
  377. if (ref_object.variable_and_value != VAR_OR_VALUE_TYPES.only_value) {
  378. menu_var_or_value.dropdown({
  379. onChange: function(value, text, $selectedItem) {
  380. dom_object.find('.var_name').remove();
  381. switch ($selectedItem.data('option')) {
  382. case VAR_OR_VALUE_TYPES.only_function:
  383. openInputToFunction(command, ref_object, dom_object, menu_var_or_value, function_obj, $($selectedItem).data('function_reference'), expression_element);
  384. break;
  385. case VAR_OR_VALUE_TYPES.only_value:
  386. openInputToValue(command, ref_object, dom_object, menu_var_or_value, function_obj, expression_element);
  387. break;
  388. case VAR_OR_VALUE_TYPES.only_variable:
  389. openInputToVariable(command, ref_object, dom_object, menu_var_or_value, function_obj, $($selectedItem).data('variable_reference'), expression_element);
  390. break;
  391. }
  392. if ($selectedItem.data('exp')) {
  393. AttribuitionsManagement.manageExpressionElements(command, ref_object, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element);
  394. }
  395. if (command.type == Models.COMMAND_TYPES.repeatNtimes) {
  396. RepeatNTimesManagement.manageExpressionElements(command, ref_object, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element);
  397. }
  398. }
  399. });
  400. }
  401. dom_object.find('.width-dynamic').on('input', function() {
  402. var inputWidth = $(this).textWidth()+10;
  403. $(this).focus();
  404. var tmpStr = $(this).val();
  405. $(this).val('');
  406. $(this).val(tmpStr);
  407. $(this).css({
  408. width: inputWidth
  409. })
  410. }).trigger('input');
  411. }
  412. function openInputToFunction (command, ref_object, dom_object, menu_var_or_value, function_obj, function_selected, expression_element) {
  413. ref_object.function_called = function_selected;
  414. ref_object.parameters_list = [];
  415. if (function_selected.parameters_list != null && function_selected.parameters_list.length > 0) {
  416. menu_var_or_value.find('.text').text(' ');
  417. dom_object.find('.menu_var_or_value_dom').remove();
  418. var parameters_menu = '<div class="parameters_function_called"> '+function_selected.name+' <span> ( </span>';
  419. for (var j = 0; j < function_selected.parameters_list.length; j++) {
  420. parameters_menu += '<div class="parameter_'+j+'"></div>';
  421. if ((j + 1) != function_selected.parameters_list.length) {
  422. parameters_menu += ' , ';
  423. }
  424. }
  425. parameters_menu += '<span> ) </span></div>';
  426. parameters_menu = $(parameters_menu);
  427. dom_object.append(parameters_menu);
  428. for (var j = 0; j < function_selected.parameters_list.length; j++) {
  429. var temp = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  430. ref_object.parameters_list.push(temp);
  431. renderMenu(command, temp, parameters_menu.find('.parameter_'+j), function_obj, 2, expression_element);
  432. }
  433. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  434. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  435. if (command.type == Models.COMMAND_TYPES.attribution) {
  436. context_menu += '<div class="item"><i class="dropdown icon"></i>' + LocalizedStrings.getUI('text_change');
  437. context_menu += '<div class="menu">';
  438. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.exp_op_exp+'">EXP OP EXP</div>';
  439. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.op_exp+'">OP EXP</div>';
  440. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.par_exp_par+'">( EXP )</div>';
  441. context_menu += '</div></div>';
  442. }
  443. context_menu += '</div></div>';
  444. context_menu = $(context_menu);
  445. context_menu.insertAfter( dom_object.find('.parameters_function_called') );
  446. context_menu.dropdown({
  447. onChange: function(value, text, $selectedItem) {
  448. if ($selectedItem.data('clear')) {
  449. console.log('PP7');
  450. dom_object.text('');
  451. ref_object.content = null;
  452. ref_object.row = null;
  453. ref_object.column = null;
  454. delete ref_object.function_called;
  455. delete ref_object.parameters_list;
  456. renderMenu(command, ref_object, dom_object, function_obj, 2, expression_element);
  457. }
  458. if ($selectedItem.data('exp')) {
  459. AttribuitionsManagement.manageExpressionElements(command, ref_object, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element);
  460. }
  461. }
  462. });
  463. } else {
  464. menu_var_or_value.find('.text').text(' ');
  465. dom_object.find('.menu_var_or_value_dom').remove();
  466. var parameters_menu = '<div class="parameters_function_called"> '+function_selected.name+' <span> ( </span>';
  467. parameters_menu += '<span> ) </span></div>';
  468. parameters_menu = $(parameters_menu);
  469. dom_object.append(parameters_menu);
  470. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  471. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  472. if (command.type == Models.COMMAND_TYPES.attribution) {
  473. context_menu += '<div class="item"><i class="dropdown icon"></i>' + LocalizedStrings.getUI('text_change');
  474. context_menu += '<div class="menu">';
  475. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.exp_op_exp+'">EXP OP EXP</div>';
  476. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.op_exp+'">OP EXP</div>';
  477. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.par_exp_par+'">( EXP )</div>';
  478. context_menu += '</div></div>';
  479. }
  480. context_menu += '</div></div>';
  481. context_menu = $(context_menu);
  482. context_menu.insertAfter( dom_object.find('.parameters_function_called') );
  483. context_menu.dropdown({
  484. onChange: function(value, text, $selectedItem) {
  485. if ($selectedItem.data('clear')) {
  486. console.log('PP8');
  487. dom_object.text('');
  488. ref_object.content = null;
  489. ref_object.row = null;
  490. ref_object.column = null;
  491. delete ref_object.function_called;
  492. delete ref_object.parameters_list;
  493. renderMenu(command, ref_object, dom_object, function_obj, 2, expression_element);
  494. }
  495. if ($selectedItem.data('exp')) {
  496. AttribuitionsManagement.manageExpressionElements(command, ref_object, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element);
  497. }
  498. }
  499. });
  500. }
  501. if (command.type == Models.COMMAND_TYPES.attribution) {
  502. AttribuitionsManagement.renderMenuOperations(command, ref_object, dom_object, menu_var_or_value, function_obj);
  503. }
  504. if (command.type == Models.COMMAND_TYPES.writer) {
  505. WritersManagement.addContent(command, ref_object, dom_object, menu_var_or_value, function_obj, ref_object.content);
  506. }
  507. }
  508. function openInputToVariable (command, ref_object, dom_object, menu_var_or_value, function_obj, variable_selected, expression_element) {
  509. ref_object.content = variable_selected;
  510. menu_var_or_value.find('.text').text(' ');
  511. dom_object.find('.menu_var_or_value_dom').remove();
  512. var variable_render = '<div class="variable_rendered"> <span class="var_name">'+variable_selected.name+'</span>';
  513. if (variable_selected.dimensions == 1) {
  514. variable_render += ' <span>[ </span> <div class="column_container"></div> <span> ]</span>';
  515. }
  516. if (variable_selected.dimensions == 2) {
  517. variable_render += ' <span>[ </span> <div class="row_container"></div> <span> ]</span> ';
  518. variable_render += ' <span>[ </span> <div class="column_container"></div> <span> ]</span>';
  519. }
  520. variable_render += '</div>';
  521. variable_render = $(variable_render);
  522. dom_object.append(variable_render);
  523. if (variable_selected.dimensions == 1) {
  524. ref_object.column = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  525. renderMenu(command, ref_object.column, variable_render.find('.column_container'), function_obj, 2, expression_element);
  526. }
  527. if (variable_selected.dimensions == 2) {
  528. ref_object.row = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  529. renderMenu(command, ref_object.row, variable_render.find('.row_container'), function_obj, 2, expression_element);
  530. ref_object.column = new Models.VariableValueMenu(VAR_OR_VALUE_TYPES.all, null, null, null, true);
  531. renderMenu(command, ref_object.column, variable_render.find('.column_container'), function_obj, 2, expression_element);
  532. }
  533. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  534. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  535. if (command.type == Models.COMMAND_TYPES.attribution) {
  536. context_menu += '<div class="item"><i class="dropdown icon"></i>' + LocalizedStrings.getUI('text_change');
  537. context_menu += '<div class="menu">';
  538. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.exp_op_exp+'">EXP OP EXP</div>';
  539. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.op_exp+'">OP EXP</div>';
  540. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.par_exp_par+'">( EXP )</div>';
  541. context_menu += '</div></div>';
  542. }
  543. context_menu += '</div></div>';
  544. context_menu = $(context_menu);
  545. context_menu.insertAfter( dom_object.find('.variable_rendered') );
  546. context_menu.dropdown({
  547. onChange: function(value, text, $selectedItem) {
  548. if ($selectedItem.data('clear')) {
  549. console.log('PP9');
  550. dom_object.text('');
  551. ref_object.content = null;
  552. ref_object.row = null;
  553. ref_object.column = null;
  554. delete ref_object.function_called;
  555. delete ref_object.parameters_list;
  556. renderMenu(command, ref_object, dom_object, function_obj, 2, expression_element);
  557. }
  558. if ($selectedItem.data('exp')) {
  559. AttribuitionsManagement.manageExpressionElements(command, ref_object, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element);
  560. }
  561. if (command.type == Models.COMMAND_TYPES.repeatNtimes) {
  562. RepeatNTimesManagement.manageClearExpressionElements(command, ref_object, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element);
  563. }
  564. }
  565. });
  566. if (command.type == Models.COMMAND_TYPES.attribution) {
  567. AttribuitionsManagement.renderMenuOperations(command, ref_object, dom_object, menu_var_or_value, function_obj, variable_selected);
  568. }
  569. if (command.type == Models.COMMAND_TYPES.writer) {
  570. WritersManagement.addContent(command, ref_object, dom_object, menu_var_or_value, function_obj, variable_selected);
  571. }
  572. }
  573. function openInputToValue (command, ref_object, dom_object, menu_var_or_value, function_obj, expression_element) {
  574. if (ref_object.content == null) {
  575. ref_object.content = "";
  576. }
  577. menu_var_or_value.find('.text').text(' ');
  578. var field = $('<input type="text" size="2" class="width-dynamic-minus" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />');
  579. field.insertBefore(dom_object.find('.menu_var_or_value_dom'));
  580. var rendered = $('<div class="value_rendered"></div>');
  581. rendered.insertBefore(field);
  582. field.focus();
  583. field.val(ref_object.content);
  584. var context_menu = '<div class="ui dropdown context_menu_clear"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  585. context_menu += '<div class="item" data-clear="true">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  586. if (command.type == Models.COMMAND_TYPES.attribution) {
  587. context_menu += '<div class="item"><i class="dropdown icon"></i>' + LocalizedStrings.getUI('text_change');
  588. context_menu += '<div class="menu">';
  589. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.exp_op_exp+'">EXP OP EXP</div>';
  590. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.op_exp+'">OP EXP</div>';
  591. context_menu += '<div class="item" data-exp="'+Models.EXPRESSION_ELEMENTS.par_exp_par+'">( EXP )</div>';
  592. context_menu += '</div></div>';
  593. }
  594. context_menu += '</div></div>';
  595. context_menu = $(context_menu);
  596. dom_object.find('.menu_var_or_value_dom').remove();
  597. if (ref_object.variable_and_value != VAR_OR_VALUE_TYPES.only_value) {
  598. context_menu.insertAfter( field );
  599. }
  600. context_menu.dropdown({
  601. onChange: function(value, text, $selectedItem) {
  602. if ($selectedItem.data('clear')) {
  603. console.log('PP10');
  604. dom_object.text('');
  605. dom_object.find('.value_rendered').remove();
  606. dom_object.find('.context_menu_clear').remove();
  607. dom_object.find('.width-dynamic-minus').remove();
  608. ref_object.content = null;
  609. ref_object.row = null;
  610. ref_object.column = null;
  611. delete ref_object.function_called;
  612. delete ref_object.parameters_list;
  613. renderMenu(command, ref_object, dom_object, function_obj, 2, expression_element);
  614. }
  615. if ($selectedItem.data('exp')) {
  616. AttribuitionsManagement.manageExpressionElements(command, ref_object, dom_object, menu_var_or_value, function_obj, $selectedItem, expression_element);
  617. }
  618. }
  619. });
  620. dom_object.find('.width-dynamic-minus').focusout(function() {
  621. if ($(this).val().trim()) {
  622. ref_object.content = $(this).val().trim();
  623. }
  624. rendered.text(ref_object.content);
  625. $(this).remove();
  626. });
  627. dom_object.find('.width-dynamic-minus').on('keydown', function(e) {
  628. var code = e.keyCode || e.which;
  629. if(code == 13) {
  630. if ($(this).val().trim()) {
  631. ref_object.content = $(this).val().trim();
  632. }
  633. rendered.text(ref_object.content);
  634. $(this).remove();
  635. }
  636. if(code == 27) {
  637. rendered.text(ref_object.content);
  638. $(this).remove();
  639. }
  640. });
  641. rendered.on('click', function(e) {
  642. console.log("TTT2");
  643. rendered.remove();
  644. rendered.empty();
  645. rendered.remove();
  646. dom_object.empty();
  647. dom_object.append('<span class="menu_var_or_value_dom"> </span>');
  648. openInputToValue(command, ref_object, dom_object, menu_var_or_value, function_obj, expression_element)
  649. });
  650. if (command.type == Models.COMMAND_TYPES.attribution) {
  651. AttribuitionsManagement.renderMenuOperations(command, ref_object, dom_object, menu_var_or_value, function_obj);
  652. }
  653. if (command.type == Models.COMMAND_TYPES.writer) {
  654. WritersManagement.addContent(command, ref_object, dom_object, menu_var_or_value, function_obj, ref_object.content);
  655. }
  656. }
  657. $.fn.textWidth = function(text, font) {
  658. if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
  659. $.fn.textWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));
  660. return $.fn.textWidth.fakeEl.width();
  661. };