variables.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. import { Types } from './types';
  2. import * as Models from './ivprog_elements';
  3. import { LocalizedStrings } from './../services/localizedStringsService';
  4. import * as Utils from './utils';
  5. import { registerUserEvent, registerSystemEvent, ActionTypes } from "./../services/userLog";
  6. import { isValidIdentifier } from "./../util/utils";
  7. var counter_new_variables = 0;
  8. export function addVariable (function_obj, function_container, is_in_click = false) {
  9. var new_var = new Models.Variable(Types.INTEGER, LocalizedStrings.getUI('new_variable') + '_' + counter_new_variables, 0);
  10. if (function_obj.variables_list == null) {
  11. function_obj.variables_list = [];
  12. }
  13. function_obj.variables_list.push(new_var);
  14. counter_new_variables ++;
  15. registerUserEvent(function_obj.name, ActionTypes.INSERT_FUNCTION_VAR, new_var.name, Types.INTEGER, 0);
  16. var newe = renderVariable(function_container, new_var, function_obj);
  17. if (is_in_click) {
  18. newe.css('display', 'none');
  19. newe.fadeIn();
  20. }
  21. }
  22. function updateName (variable_obj, new_name, variable_obj_dom, function_obj) {
  23. if (variable_obj.name == new_name) {
  24. return;
  25. }
  26. if (isValidIdentifier(new_name)) {
  27. if (variableNameAlreadyExists(new_name, function_obj)) {
  28. Utils.renderErrorMessage(variable_obj_dom.find('.editing_name_var'), LocalizedStrings.getError('inform_valid_variable_duplicated', [new_name, function_obj.name]));
  29. } else {
  30. registerUserEvent(function_obj.name, ActionTypes.RENAME_FUNCTION_VAR, variable_obj.name, new_name);
  31. variable_obj.name = new_name;
  32. }
  33. } else {
  34. Utils.renderErrorMessage(variable_obj_dom.find('.editing_name_var'), LocalizedStrings.getError('inform_valid_identifier'));
  35. }
  36. }
  37. function variableNameAlreadyExists (name_var, function_obj) {
  38. if (function_obj.parameters_list) {
  39. for (var i = 0; i < function_obj.parameters_list.length; i++) {
  40. if (function_obj.parameters_list[i].name == name_var) {
  41. return true;
  42. }
  43. }
  44. }
  45. if (function_obj.variables_list) {
  46. for (var i = 0; i < function_obj.variables_list.length; i++) {
  47. if (function_obj.variables_list[i].name == name_var) {
  48. return true;
  49. }
  50. }
  51. }
  52. return false;
  53. }
  54. function removeVariable (variable_obj, variable_container, function_name) {
  55. var function_associated = variable_container.data('associatedFunction');
  56. registerUserEvent(function_name, ActionTypes.REMOVE_FUNCTION_VAR, variable_obj.name);
  57. var index = function_associated.variables_list.indexOf(variable_obj);
  58. if (index > -1) {
  59. window.insertContext = true;
  60. delete function_associated.variables_list[index];
  61. function_associated.variables_list.splice(index, 1);
  62. }
  63. variable_container.children().off();
  64. variable_container.off();
  65. variable_container.fadeOut();
  66. }
  67. function updateType (variable_obj, new_type, function_name, new_dimensions = 0) {
  68. variable_obj.type = new_type;
  69. variable_obj.dimensions = new_dimensions;
  70. if (new_dimensions > 0) {
  71. variable_obj.rows = new_dimensions;
  72. variable_obj.columns = 2;
  73. }
  74. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_TYPE, variable_obj.name,
  75. new_type, new_dimensions, variable_obj.rows, variable_obj.columns);
  76. updateInitialValues(variable_obj, function_name);
  77. }
  78. function addHandlers (variable_obj, variable_container, function_obj) {
  79. // Manage variable name:
  80. variable_container.find( ".editing_name_var" ).on('click', function(e){
  81. registerUserEvent(function_obj.name, ActionTypes.ENTER_CHANGE_VAR_NAME, variable_obj.name);
  82. enableNameUpdate(variable_obj, variable_container, function_obj);
  83. });
  84. // Menu to change type:
  85. variable_container.find('.ui.dropdown.variable_type').dropdown({
  86. onChange: function(_, __, $selectedItem) {
  87. if ($selectedItem.data('dimensions')) {
  88. updateType(variable_obj, Types[$selectedItem.data('type')], function_obj.name, $selectedItem.data('dimensions'));
  89. } else {
  90. updateType(variable_obj, Types[$selectedItem.data('type')], function_obj.name);
  91. }
  92. renderValues(variable_obj, variable_container, function_obj.name);
  93. },
  94. selectOnKeydown: false
  95. });
  96. // Remove variable:
  97. variable_container.find( ".remove_variable" ).on('click', function(e){
  98. removeVariable(variable_obj, variable_container, function_obj.name);
  99. });
  100. }
  101. export function renderVariable (function_container, new_var, function_obj) {
  102. var element = '<div class="ui label variable_container pink"><i class="ui icon ellipsis vertical inverted"></i>';
  103. element += '<div class="ui dropdown variable_type">';
  104. if (new_var.dimensions == 1) {
  105. element += '<div class="text">'+ LocalizedStrings.getUI('vector') + ': ' + LocalizedStrings.getUI(`type_${new_var.type.toLowerCase()}`);
  106. element += ' [ ] </div>';
  107. } else if (new_var.dimensions == 2) {
  108. element += '<div class="text">'+ LocalizedStrings.getUI('matrix') + ': ' + LocalizedStrings.getUI(`type_${new_var.type.toLowerCase()}`);
  109. element += ' [ ] [ ] </div>';
  110. } else {
  111. element += '<div class="text">' + LocalizedStrings.getUI(`type_${new_var.type.toLowerCase()}`) + '</div>';
  112. }
  113. element += '<div class="menu">';
  114. for (var tm in Types) {
  115. if (tm == Types.VOID.toUpperCase()) {
  116. continue;
  117. }
  118. element += '<div class="item ' + (new_var.type == tm.toLowerCase() && new_var.dimensions == 0 ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
  119. }
  120. element += '<div class="item ' + (new_var.dimensions == 1 ? ' selected ' : '') + '">'
  121. + '<i class="dropdown icon"></i>' + LocalizedStrings.getUI('vector')
  122. + '<div class="menu">';
  123. for (var tm in Types) {
  124. if (tm == Types.VOID.toUpperCase()) {
  125. continue;
  126. }
  127. element += '<div class="item ' + (new_var.type == tm.toLowerCase() && new_var.dimensions == 1 ? ' selected ' : '') + '" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+' [ ] " data-type="'+tm+'" data-dimensions="1"> '+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
  128. }
  129. element += '</div></div>';
  130. element += '<div class="item ' + (new_var.dimensions == 2 ? ' selected ' : '') + '">'
  131. + '<i class="dropdown icon"></i>' + LocalizedStrings.getUI('matrix')
  132. + '<div class="menu">';
  133. for (var tm in Types) {
  134. if (tm == Types.VOID.toUpperCase()) {
  135. continue;
  136. }
  137. element += '<div class="item ' + (new_var.type == tm.toLowerCase() && new_var.dimensions == 2 ? ' selected ' : '') + '" data-text="'+ LocalizedStrings.getUI('matrix')+':'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+' [ ] " data-type="'+tm+'" data-dimensions="2"> '+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
  138. }
  139. element += '</div></div>';
  140. element += '</div></div> ';
  141. element += '<div class="editing_name_var"><span class="span_name_variable enable_edit_name_variable">'+new_var.name+'</span> </div>';
  142. element += ' <span class="character_equals"> <&#8212; </span> <div class="ui div_valor_var">'+new_var.value+'</div>';
  143. element += ' <i class="yellow inverted icon times remove_variable"></i></div>';
  144. element = $(element);
  145. element.data('associatedFunction', function_obj);
  146. function_container.find('.variables_list_div').append(element);
  147. addHandlers(new_var, element, function_obj);
  148. renderValues(new_var, element, function_obj.name);
  149. return element;
  150. }
  151. function updateColumnsAndRowsText (variable_container, variable_var) {
  152. var prev = variable_container.find('.text').text().split('[');
  153. if (prev.length == 2) {
  154. var ff = prev[0] + '[ ' + variable_var.columns + ' ] ';
  155. variable_container.find('.text').empty();
  156. variable_container.find('.text').text(ff);
  157. }
  158. if (prev.length == 3) {
  159. var ff = prev[0] + '[ ' + variable_var.columns + ' ] [ ' + variable_var.rows + ' ] ';
  160. variable_container.find('.text').empty();
  161. variable_container.find('.text').text(ff);
  162. }
  163. }
  164. function renderValues (new_var, variable_container, function_name) {
  165. var ret = "";
  166. var j = 0;
  167. if (new_var.dimensions == 0) {
  168. if (new_var.type == Types.REAL) {
  169. ret += '<div class="created_div_valor_var"><span class="span_value_variable simple_var">'+new_var.value.toFixed(1)+'</span> </div> ';
  170. } else {
  171. if (new_var.type == Types.BOOLEAN) {
  172. ret += '<div class="created_div_valor_var"><span class="span_value_variable boolean_simple_type">'+LocalizedStrings.getUI(`logic_value_${new_var.value}`)+'</span> </div> ';
  173. } else {
  174. ret += '<div class="created_div_valor_var"><span class="span_value_variable simple_var">'+new_var.value+'</span> </div> ';
  175. }
  176. }
  177. } else {
  178. ret += '<table class="tabela_var">';
  179. if (new_var.dimensions == 1) {
  180. ret += '<tr>';
  181. if (new_var.type == Types.REAL) {
  182. for (var k = 0; k < new_var.columns; k++) {
  183. ret += '<td><span class="span_value_variable vector_var" data-index="'+k+'">'+new_var.value[k].toFixed(1)+'</span></td>';
  184. }
  185. } else {
  186. for (var k = 0; k < new_var.columns; k++) {
  187. if (new_var.type == Types.BOOLEAN) {
  188. ret += '<td><span class="span_value_variable boolean_vector_var" data-index="'+k+'">'+LocalizedStrings.getUI(`logic_value_${new_var.value[k]}`)+'</span></td>';
  189. } else {
  190. ret += '<td><span class="span_value_variable vector_var" data-index="'+k+'">'+new_var.value[k]+'</span>'+'</td>';
  191. }
  192. }
  193. }
  194. ret += '</tr>';
  195. ret += '</table>';
  196. ret += '<div class="buttons_manage_columns"><i class="ui icon minus square outline remove_global_vector_column"></i>'
  197. + ' <i class="ui icon plus square outline add_global_vector_column"></i></div>';
  198. }
  199. if (new_var.dimensions == 2) {
  200. if (new_var.type == Types.REAL) {
  201. for (var l = 0; l < new_var.rows; l++) {
  202. ret += '<tr>';
  203. for (var k = 0; k < new_var.columns; k++) {
  204. ret += '<td><span class="span_value_variable matrix_var" data-index="'+k+'" data-row="'+l+'">'+new_var.value[l][k].toFixed(1)+'</span>'+'</td>';
  205. }
  206. ret += '</tr>';
  207. }
  208. } else {
  209. for (var l = 0; l < new_var.rows; l++) {
  210. ret += '<tr>';
  211. for (var k = 0; k < new_var.columns; k++) {
  212. if (new_var.type == Types.BOOLEAN) {
  213. ret += '<td><span class="span_value_variable boolean_matrix_var" data-index="'+k+'" data-row="'+l+'">'+LocalizedStrings.getUI(`logic_value_${new_var.value[l][k]}`)+'</span></td>';
  214. } else {
  215. ret += '<td><span class="span_value_variable matrix_var" data-index="'+k+'" data-row="'+l+'">'+new_var.value[l][k]+'</span></td>';
  216. }
  217. }
  218. ret += '</tr>';
  219. }
  220. }
  221. if (new_var.rows == 0) {
  222. ret += '<tr><td></td></tr>';
  223. }
  224. ret += '<tr><td colspan="'+new_var.columns+'" class="tr_manage_lines"><i class="ui icon minus square outline remove_global_matrix_line"></i>'
  225. + ' <i class="ui icon plus square outline add_global_matrix_line"></i></td></tr>';
  226. ret += '</table>';
  227. ret += '<div class="buttons_manage_columns"><i class="ui icon minus square outline remove_global_matrix_column"></i>'
  228. + ' <i class="ui icon plus square outline add_global_matrix_column"></i></div>';
  229. }
  230. }
  231. $( variable_container ).find( ".div_valor_var" ).html('');
  232. ret = $(ret);
  233. $(ret).find('.span_value_variable'). data('associatedOject', new_var);
  234. $( ret ).find( ".boolean_simple_type" ).on('click', function(e){
  235. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, new_var.name, new_var.value);
  236. alternateBooleanValue(new_var, this.parentNode);
  237. });
  238. $( ret ).find( ".simple_var" ).on('click', function(e){
  239. registerUserEvent(function_name, ActionTypes.ENTER_CHANGE_VAR_VALUE, new_var.name);
  240. enableValueUpdate(new_var, this.parentNode, function_name);
  241. });
  242. $( ret ).find( ".boolean_vector_var" ).on('click', function(e){
  243. alternateBooleanVectorValue(new_var, $(this).data('index'), this.parentNode);
  244. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, new_var.name,
  245. new_var.value);
  246. });
  247. $( ret ).find( ".vector_var" ).on('click', function(e){
  248. enableVectorValueUpdate(new_var, $(this).data('index'), this.parentNode, function_name);
  249. });
  250. $( ret ).find( ".remove_global_vector_column" ).on('click', function(e){
  251. removeColumnVector(new_var);
  252. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_TYPE, new_var.name,
  253. new_var.type, new_var.dimensions, new_var.rows, new_var.columns);
  254. registerSystemEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, new_var.name,
  255. new_var.value);
  256. $( variable_container ).find( ".div_valor_var" ).html('');
  257. renderValues(new_var, variable_container, function_name);
  258. });
  259. $( ret ).find( ".add_global_vector_column" ).on('click', function(e){
  260. addColumnVector(new_var);
  261. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_TYPE, new_var.name,
  262. new_var.type, new_var.dimensions, new_var.rows, new_var.columns);
  263. registerSystemEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, new_var.name,
  264. new_var.value);
  265. $( variable_container ).find( ".div_valor_var" ).html('');
  266. renderValues(new_var, variable_container, function_name);
  267. });
  268. $( ret ).find( ".remove_global_matrix_column" ).on('click', function(e){
  269. removeColumnMatrix(new_var);
  270. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_TYPE, new_var.name,
  271. new_var.type, new_var.dimensions, new_var.rows, new_var.columns);
  272. registerSystemEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, new_var.name,
  273. new_var.value);
  274. $( variable_container ).find( ".div_valor_var" ).html('');
  275. renderValues(new_var, variable_container, function_name);
  276. });
  277. $( ret ).find( ".add_global_matrix_column" ).on('click', function(e){
  278. addColumnMatrix(new_var);
  279. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_TYPE, new_var.name,
  280. new_var.type, new_var.dimensions, new_var.rows, new_var.columns);
  281. registerSystemEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, new_var.name,
  282. new_var.value);
  283. $( variable_container ).find( ".div_valor_var" ).html('');
  284. renderValues(new_var, variable_container, function_name);
  285. });
  286. $( ret ).find( ".remove_global_matrix_line" ).on('click', function(e){
  287. removeLineMatrix(new_var);
  288. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_TYPE, new_var.name,
  289. new_var.type, new_var.dimensions, new_var.rows, new_var.columns);
  290. registerSystemEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, new_var.name,
  291. new_var.value);
  292. $( variable_container ).find( ".div_valor_var" ).html('');
  293. renderValues(new_var, variable_container, function_name);
  294. });
  295. $( ret ).find( ".add_global_matrix_line" ).on('click', function(e){
  296. addLineMatrix(new_var);
  297. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_TYPE, new_var.name,
  298. new_var.type, new_var.dimensions, new_var.rows, new_var.columns);
  299. registerSystemEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, new_var.name,
  300. new_var.value);
  301. $( variable_container ).find( ".div_valor_var" ).html('');
  302. renderValues(new_var, variable_container, function_name);
  303. });
  304. $( ret ).find( ".boolean_matrix_var" ).on('click', function(e){
  305. alternateBooleanMatrixValue(new_var, $(this).data('row'), $(this).data('index'), this.parentNode);
  306. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, new_var.name,
  307. new_var.value);
  308. });
  309. $( ret ).find( ".matrix_var" ).on('click', function(e){
  310. registerUserEvent(function_name, ActionTypes.ENTER_CHANGE_VAR_VALUE, new_var.name);
  311. enableMatrixValueUpdate(new_var, $(this).data('row'), $(this).data('index'), this.parentNode, function_name);
  312. });
  313. $( variable_container ).find( ".div_valor_var" ).append(ret);
  314. updateColumnsAndRowsText(variable_container, new_var);
  315. }
  316. function alternateBooleanMatrixValue (var_obj, row, index, value_container) {
  317. var_obj.value[row][index] = !var_obj.value[row][index];
  318. $(value_container).find('.span_value_variable').text(LocalizedStrings.getUI(`logic_value_${var_obj.value[row][index]}`));
  319. }
  320. function addLineMatrix (var_obj) {
  321. var_obj.rows ++;
  322. if (var_obj.type == Types.INTEGER) {
  323. var n_l = [];
  324. for (var i = 0; i < var_obj.columns; i++) {
  325. n_l.push(0);
  326. }
  327. var_obj.value.push(n_l);
  328. }
  329. if (var_obj.type == Types.REAL) {
  330. var n_l = [];
  331. for (i = 0; i < var_obj.columns; i++) {
  332. n_l.push(0.0);
  333. }
  334. var_obj.value.push(n_l);
  335. }
  336. if (var_obj.type == Types.TEXT) {
  337. var n_l = [];
  338. for (i = 0; i < var_obj.columns; i++) {
  339. n_l.push(LocalizedStrings.getUI('textvar_default_value'));
  340. }
  341. var_obj.value.push(n_l);
  342. }
  343. if (var_obj.type == Types.BOOLEAN) {
  344. var n_l = [];
  345. for (i = 0; i < var_obj.columns; i++) {
  346. n_l.push(true);
  347. }
  348. var_obj.value.push(n_l);
  349. }
  350. if (var_obj.type == Types.CHAR) {
  351. var n_l = [];
  352. for (i = 0; i < var_obj.columns; i++) {
  353. n_l.push(LocalizedStrings.getUI('charvar_default_value'));
  354. }
  355. var_obj.value.push(n_l);
  356. }
  357. }
  358. function removeLineMatrix (var_obj) {
  359. if (var_obj.rows == 0) {
  360. return;
  361. }
  362. var_obj.rows --;
  363. var_obj.value.splice(var_obj.value.length - 1, 1);
  364. }
  365. function addColumnMatrix (var_obj) {
  366. var_obj.columns ++;
  367. if (var_obj.type == Types.INTEGER) {
  368. for (var i = 0; i < var_obj.rows; i++) {
  369. var_obj.value[i].push(0);
  370. }
  371. }
  372. if (var_obj.type == Types.REAL) {
  373. for (var i = 0; i < var_obj.rows; i++) {
  374. var_obj.value[i].push(0.0);
  375. }
  376. }
  377. if (var_obj.type == Types.TEXT) {
  378. for (var i = 0; i < var_obj.rows; i++) {
  379. var_obj.value[i].push(LocalizedStrings.getUI('textvar_default_value'));
  380. }
  381. }
  382. if (var_obj.type == Types.BOOLEAN) {
  383. for (var i = 0; i < var_obj.rows; i++) {
  384. var_obj.value[i].push(true);
  385. }
  386. }
  387. if (var_obj.type == Types.CHAR) {
  388. for (var i = 0; i < var_obj.rows; i++) {
  389. var_obj.value[i].push(LocalizedStrings.getUI('charvar_default_value'));
  390. }
  391. }
  392. }
  393. function removeColumnMatrix (var_obj) {
  394. if (var_obj.columns == 0) {
  395. return;
  396. }
  397. var_obj.columns --;
  398. for (var i = 0; i < var_obj.rows; i++) {
  399. var_obj.value[i].splice(var_obj.value[i].length - 1, 1);
  400. }
  401. }
  402. function addColumnVector (var_obj) {
  403. var_obj.columns ++;
  404. if (var_obj.type == Types.INTEGER) {
  405. var_obj.value.push(0);
  406. }
  407. if (var_obj.type == Types.REAL) {
  408. var_obj.value.push(0.0);
  409. }
  410. if (var_obj.type == Types.TEXT) {
  411. var_obj.value.push(LocalizedStrings.getUI('textvar_default_value'));
  412. }
  413. if (var_obj.type == Types.BOOLEAN) {
  414. var_obj.value.push(true);
  415. }
  416. if (var_obj.type == Types.CHAR) {
  417. var_obj.value.push(LocalizedStrings.getUI('charvar_default_value'));
  418. }
  419. }
  420. function removeColumnVector (var_obj) {
  421. if (var_obj.columns == 0) {
  422. return;
  423. }
  424. var_obj.columns --;
  425. var_obj.value.splice(var_obj.value.length - 1, 1);
  426. }
  427. function alternateBooleanValue (var_obj, value_container) {
  428. var_obj.value = !var_obj.value;
  429. $(value_container).find('.span_value_variable').text(LocalizedStrings.getUI(`logic_value_${var_obj.value}`));
  430. }
  431. function alternateBooleanVectorValue (var_obj, index, value_container) {
  432. var_obj.value[index] = !var_obj.value[index];
  433. $(value_container).find('.span_value_variable').text(LocalizedStrings.getUI(`logic_value_${var_obj.value[index]}`));
  434. }
  435. function updateInitialValues (variable_obj, function_name) {
  436. if (variable_obj.type == Types.INTEGER) {
  437. if (variable_obj.dimensions == 0) {
  438. variable_obj.value = 0;
  439. }
  440. if (variable_obj.dimensions == 1) {
  441. variable_obj.value = [0, 0];
  442. }
  443. if (variable_obj.dimensions == 2) {
  444. variable_obj.value = [[0, 0], [0, 0]];
  445. }
  446. }
  447. if (variable_obj.type == Types.REAL) {
  448. if (variable_obj.dimensions == 0) {
  449. variable_obj.value = 0.0;
  450. }
  451. if (variable_obj.dimensions == 1) {
  452. variable_obj.value = [0.0, 0.0];
  453. }
  454. if (variable_obj.dimensions == 2) {
  455. variable_obj.value = [[0.0, 0.0], [0.0, 0.0]];
  456. }
  457. }
  458. if (variable_obj.type == Types.TEXT) {
  459. if (variable_obj.dimensions == 0) {
  460. variable_obj.value = LocalizedStrings.getUI('textvar_default_value');
  461. }
  462. if (variable_obj.dimensions == 1) {
  463. variable_obj.value = [LocalizedStrings.getUI('textvar_default_value'), LocalizedStrings.getUI('textvar_default_value')];
  464. }
  465. if (variable_obj.dimensions == 2) {
  466. variable_obj.value = [[LocalizedStrings.getUI('textvar_default_value'), LocalizedStrings.getUI('textvar_default_value')],
  467. [LocalizedStrings.getUI('textvar_default_value'), LocalizedStrings.getUI('textvar_default_value')]];
  468. }
  469. }
  470. if (variable_obj.type == Types.BOOLEAN) {
  471. if (variable_obj.dimensions == 0) {
  472. variable_obj.value = true;
  473. }
  474. if (variable_obj.dimensions == 1) {
  475. variable_obj.value = [true, true];
  476. }
  477. if (variable_obj.dimensions == 2) {
  478. variable_obj.value = [[true, true], [true, true]];
  479. }
  480. }
  481. if (variable_obj.type == Types.CHAR) {
  482. if (variable_obj.dimensions == 0) {
  483. variable_obj.value = 'A';
  484. }
  485. if (variable_obj.dimensions == 1) {
  486. variable_obj.value = ['A', 'A'];
  487. }
  488. if (variable_obj.dimensions == 2) {
  489. variable_obj.value = [['A', 'A'], ['A', 'A']];
  490. }
  491. }
  492. registerSystemEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, variable_obj.name, variable_obj.value);
  493. }
  494. var opened_name_value_vector_global_ = false;
  495. var opened_input_value_vector_global_ = null;
  496. function enableVectorValueUpdate (var_obj, index, parent_node, function_name) {
  497. if (opened_name_value_vector_global_) {
  498. opened_input_value_vector_global_.focus();
  499. return;
  500. }
  501. parent_node = $(parent_node);
  502. opened_name_value_vector_global_ = true;
  503. parent_node.find('.span_value_variable').text('');
  504. var input_field;
  505. if (var_obj.type == Types.REAL) {
  506. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  507. + var_obj.value[index].toFixed(1) + "' />" );
  508. input_field.insertBefore(parent_node.find('.span_value_variable'));
  509. } else {
  510. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  511. + var_obj.value[index] + "' />" );
  512. input_field.insertBefore(parent_node.find('.span_value_variable'));
  513. }
  514. input_field.on('input', function() {
  515. var inputWidth = input_field.textWidth()+10;
  516. opened_input_value_vector_global_ = input_field;
  517. input_field.focus();
  518. var tmpStr = input_field.val();
  519. input_field.val('');
  520. input_field.val(tmpStr);
  521. input_field.css({
  522. width: inputWidth
  523. })
  524. }).trigger('input');
  525. input_field.focusout(function() {
  526. let changed = false;
  527. /// update array:
  528. if (input_field.val().trim()) {
  529. if (var_obj.type == Types.REAL) {
  530. var_obj.value[index] = parseFloat(input_field.val().trim());
  531. parent_node.find('.span_value_variable').text(var_obj.value[index].toFixed(1));
  532. } else {
  533. if (var_obj.type == Types.INTEGER) {
  534. var_obj.value[index] = parseInt(input_field.val().trim());
  535. } else {
  536. var_obj.value[index] = input_field.val().trim();
  537. }
  538. parent_node.find('.span_value_variable').text(var_obj.value[index]);
  539. }
  540. changed = true;
  541. } else {
  542. if (var_obj.type == Types.REAL) {
  543. parent_node.find('.span_value_variable').text(var_obj.value[index].toFixed(1));
  544. } else {
  545. parent_node.find('.span_value_variable').text(var_obj.value[index]);
  546. }
  547. }
  548. if (var_obj.type == Types.TEXT) {
  549. var_obj.value[index] = input_field.val();
  550. changed = true;
  551. parent_node.find('.span_value_variable').text(var_obj.value[index]);
  552. }
  553. if (changed) {
  554. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, var_obj.name,
  555. var_obj.value);
  556. }
  557. input_field.off();
  558. input_field.remove();
  559. /// update elements:
  560. opened_name_value_vector_global_ = false;
  561. opened_input_value_vector_global_ = false;
  562. });
  563. input_field.on('keydown', function(e) {
  564. const code = e.keyCode || e.which;
  565. let changed = false;
  566. if(code == 13) {
  567. if (input_field.val().trim()) {
  568. if (var_obj.type == Types.REAL) {
  569. var_obj.value[index] = parseFloat(input_field.val().trim());
  570. parent_node.find('.span_value_variable').text(var_obj.value[index].toFixed(1));
  571. } else {
  572. if (var_obj.type == Types.INTEGER) {
  573. var_obj.value[index] = parseInt(input_field.val().trim());
  574. } else {
  575. var_obj.value[index] = input_field.val().trim();
  576. }
  577. parent_node.find('.span_value_variable').text(var_obj.value[index]);
  578. }
  579. changed = true;
  580. } else {
  581. if (var_obj.type == Types.REAL) {
  582. parent_node.find('.span_value_variable').text(var_obj.value[index].toFixed(1));
  583. } else {
  584. parent_node.find('.span_value_variable').text(var_obj.value[index]);
  585. }
  586. }
  587. if (var_obj.type == Types.TEXT) {
  588. var_obj.value[index] = input_field.val();
  589. changed = true;
  590. parent_node.find('.span_value_variable').text(var_obj.value[index]);
  591. }
  592. if (changed) {
  593. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, var_obj.name,
  594. var_obj.value);
  595. }
  596. input_field.off();
  597. input_field.remove();
  598. /// update elements:
  599. opened_name_value_vector_global_ = false;
  600. opened_input_value_vector_global_ = false;
  601. }
  602. if(code == 27) {
  603. if (var_obj.type == Types.REAL) {
  604. parent_node.find('.span_value_variable').text(var_obj.value[index].toFixed(1));
  605. } else {
  606. parent_node.find('.span_value_variable').text(var_obj.value[index]);
  607. }
  608. input_field.off();
  609. input_field.remove();
  610. /// update elements:
  611. opened_name_value_vector_global_ = false;
  612. opened_input_value_vector_global_ = false;
  613. }
  614. });
  615. input_field.select();
  616. }
  617. var opened_name_value_global_var = false;
  618. var opened_input_value_global_ar = null;
  619. function enableValueUpdate (var_obj, parent_node, function_name) {
  620. if (opened_name_value_global_var) {
  621. opened_input_value_global_ar.focus();
  622. return;
  623. }
  624. parent_node = $(parent_node);
  625. opened_name_value_global_var = true;
  626. var input_field;
  627. parent_node.find('.span_value_variable').text('');
  628. if (var_obj.type == Types.REAL) {
  629. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  630. + var_obj.value.toFixed(1) + "' />" );
  631. input_field.insertBefore(parent_node.find('.span_value_variable'));
  632. } else {
  633. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  634. + var_obj.value + "' />" );
  635. input_field.insertBefore(parent_node.find('.span_value_variable'));
  636. }
  637. input_field.on('input', function() {
  638. var inputWidth = input_field.textWidth()+10;
  639. opened_input_value_global_ar = input_field;
  640. input_field.focus();
  641. var tmpStr = input_field.val();
  642. input_field.val('');
  643. input_field.val(tmpStr);
  644. input_field.css({
  645. width: inputWidth
  646. })
  647. }).trigger('input');
  648. input_field.focusout(function() {
  649. /// update array:
  650. let changed = false;
  651. if (input_field.val().trim()) {
  652. if (var_obj.type == Types.REAL) {
  653. var_obj.value = parseFloat(input_field.val().trim());
  654. parent_node.find('.span_value_variable').text(var_obj.value.toFixed(1));
  655. } else{
  656. if (var_obj.type == Types.INTEGER) {
  657. var_obj.value = parseInt(input_field.val().trim());
  658. } else if (var_obj.type == Types.CHAR) {
  659. var_obj.value = input_field.val().charAt(0);
  660. } else {
  661. var_obj.value = input_field.val().trim();
  662. }
  663. parent_node.find('.span_value_variable').text(var_obj.value);
  664. }
  665. changed = true;
  666. } else {
  667. if (var_obj.type == Types.REAL) {
  668. parent_node.find('.span_value_variable').text(var_obj.value.toFixed(1));
  669. } else {
  670. parent_node.find('.span_value_variable').text(var_obj.value);
  671. }
  672. }
  673. if (var_obj.type == Types.TEXT) {
  674. var_obj.value = input_field.val();
  675. changed = true;
  676. parent_node.find('.span_value_variable').text(var_obj.value);
  677. }
  678. if (changed) {
  679. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, var_obj.name,
  680. var_obj.value);
  681. }
  682. input_field.off();
  683. input_field.remove();
  684. /// update elements:
  685. opened_name_value_global_var = false;
  686. opened_input_value_global_ar = false;
  687. });
  688. input_field.on('keydown', function(e) {
  689. const code = e.keyCode || e.which;
  690. let changed = false;
  691. if(code == 13) {
  692. if (input_field.val().trim()) {
  693. if (var_obj.type == Types.REAL) {
  694. var_obj.value = parseFloat(input_field.val().trim());
  695. parent_node.find('.span_value_variable').text(var_obj.value.toFixed(1));
  696. } else{
  697. if (var_obj.type == Types.INTEGER) {
  698. var_obj.value = parseInt(input_field.val().trim());
  699. } else if (var_obj.type == Types.CHAR) {
  700. var_obj.value = input_field.val().charAt(0);
  701. } else {
  702. var_obj.value = input_field.val().trim();
  703. }
  704. parent_node.find('.span_value_variable').text(var_obj.value);
  705. }
  706. changed = true;
  707. } else {
  708. if (var_obj.type == Types.REAL) {
  709. parent_node.find('.span_value_variable').text(var_obj.value.toFixed(1));
  710. } else {
  711. parent_node.find('.span_value_variable').text(var_obj.value);
  712. }
  713. }
  714. if (var_obj.type == Types.TEXT) {
  715. var_obj.value = input_field.val();
  716. changed = true;
  717. parent_node.find('.span_value_variable').text(var_obj.value);
  718. }
  719. if (changed) {
  720. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, var_obj.name,
  721. var_obj.value);
  722. }
  723. input_field.off();
  724. input_field.remove();
  725. /// update elements:
  726. opened_name_value_global_var = false;
  727. opened_input_value_global_ar = false;
  728. }
  729. if(code == 27) {
  730. if (var_obj.type == Types.REAL) {
  731. parent_node.find('.span_value_variable').text(var_obj.value.toFixed(1));
  732. } else{
  733. parent_node.find('.span_value_variable').text(var_obj.value);
  734. }
  735. input_field.off();
  736. input_field.remove();
  737. /// update elements:
  738. opened_name_value_global_var = false;
  739. opened_input_value_global_ar = false;
  740. }
  741. });
  742. input_field.select();
  743. }
  744. var opened_name_global = false;
  745. var opened_input_global = null;
  746. function enableNameUpdate (variable_obj, variable_container, function_obj) {
  747. if (opened_name_global) {
  748. opened_input_global.focus();
  749. return;
  750. }
  751. opened_name_global = true;
  752. variable_container.find('.span_name_variable').text('');
  753. var input_name;
  754. input_name = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+variable_obj.name+"' />" );
  755. input_name.insertBefore(variable_container.find('.span_name_variable'));
  756. input_name.on('input', function() {
  757. var inputWidth = input_name.textWidth()+10;
  758. opened_input_global = input_name;
  759. input_name.focus();
  760. var tmpStr = input_name.val();
  761. input_name.val('');
  762. input_name.val(tmpStr);
  763. input_name.css({
  764. width: inputWidth
  765. })
  766. }).trigger('input');
  767. input_name.focusout(function() {
  768. /// update array:
  769. if (input_name.val().trim().length > 0) {
  770. updateName(variable_obj, input_name.val().trim(), variable_container, function_obj);
  771. variable_container.find('.span_name_variable').text(variable_obj.name);
  772. } else {
  773. variable_container.find('.span_name_variable').text(variable_obj.name);
  774. }
  775. input_name.off();
  776. input_name.remove();
  777. /// update elements:
  778. opened_name_global = false;
  779. opened_input_global = false;
  780. });
  781. input_name.on('keydown', function(e) {
  782. var code = e.keyCode || e.which;
  783. if(code == 13) {
  784. if (input_name.val().trim().length > 0) {
  785. updateName(variable_obj, input_name.val().trim(), variable_container, function_obj);
  786. variable_container.find('.span_name_variable').text(variable_obj.name);
  787. } else {
  788. variable_container.find('.span_name_variable').text(variable_obj.name);
  789. }
  790. input_name.off();
  791. input_name.remove();
  792. /// update elements:
  793. opened_name_global = false;
  794. opened_input_global = false;
  795. }
  796. if(code == 27) {
  797. variable_container.find('.span_name_variable').text(variable_obj.name);
  798. input_name.off();
  799. input_name.remove();
  800. /// update elements:
  801. opened_name_global = false;
  802. opened_input_global = false;
  803. }
  804. });
  805. input_name.select();
  806. }
  807. var opened_name_value_matrix_global_v = false;
  808. var opened_input_value_matrix_global_v = null;
  809. function enableMatrixValueUpdate (var_obj, row, index, parent_node, function_name) {
  810. if (opened_name_value_matrix_global_v) {
  811. opened_input_value_matrix_global_v.focus();
  812. return;
  813. }
  814. parent_node = $(parent_node);
  815. opened_name_value_matrix_global_v = true;
  816. parent_node.find('.span_value_variable').text('');
  817. var input_field;
  818. if (var_obj.type == Types.REAL) {
  819. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  820. + var_obj.value[row][index].toFixed(1) + "' />" );
  821. input_field.insertBefore(parent_node.find('.span_value_variable'));
  822. } else {
  823. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  824. + var_obj.value[row][index] + "' />" );
  825. input_field.insertBefore(parent_node.find('.span_value_variable'));
  826. }
  827. input_field.on('input', function() {
  828. var inputWidth = input_field.textWidth()+10;
  829. opened_input_value_matrix_global_v = input_field;
  830. input_field.focus();
  831. var tmpStr = input_field.val();
  832. input_field.val('');
  833. input_field.val(tmpStr);
  834. input_field.css({
  835. width: inputWidth
  836. })
  837. }).trigger('input');
  838. input_field.focusout(function() {
  839. let changed = false;
  840. /// update array:
  841. if (input_field.val().trim()) {
  842. if (var_obj.type == Types.REAL) {
  843. var_obj.value[row][index] = parseFloat(input_field.val().trim());
  844. parent_node.find('.span_value_variable').text(var_obj.value[row][index].toFixed(1));
  845. } else {
  846. if (var_obj.type == Types.INTEGER) {
  847. var_obj.value[row][index] = parseInt(input_field.val().trim());
  848. } else {
  849. var_obj.value[row][index] = input_field.val().trim();
  850. }
  851. parent_node.find('.span_value_variable').text(var_obj.value[row][index]);
  852. }
  853. changed = true;
  854. } else {
  855. if (var_obj.type == Types.REAL) {
  856. parent_node.find('.span_value_variable').text(var_obj.value[row][index].toFixed(1));
  857. } else {
  858. parent_node.find('.span_value_variable').text(var_obj.value[row][index]);
  859. }
  860. }
  861. if (var_obj.type == Types.TEXT) {
  862. var_obj.value[row][index] = input_field.val();
  863. changed = true;
  864. parent_node.find('.span_value_variable').text(var_obj.value[row][index]);
  865. }
  866. if (changed) {
  867. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, var_obj.name,
  868. var_obj.value);
  869. }
  870. input_field.off();
  871. input_field.remove();
  872. /// update elements:
  873. opened_name_value_matrix_global_v = false;
  874. opened_input_value_matrix_global_v = false;
  875. });
  876. input_field.on('keydown', function(e) {
  877. const code = e.keyCode || e.which;
  878. let changed = false;
  879. if(code == 13) {
  880. if (input_field.val().trim()) {
  881. if (var_obj.type == Types.REAL) {
  882. var_obj.value[row][index] = parseFloat(input_field.val().trim());
  883. parent_node.find('.span_value_variable').text(var_obj.value[row][index].toFixed(1));
  884. } else {
  885. if (var_obj.type == Types.INTEGER) {
  886. var_obj.value[row][index] = parseInt(input_field.val().trim());
  887. } else {
  888. var_obj.value[row][index] = input_field.val().trim();
  889. }
  890. parent_node.find('.span_value_variable').text(var_obj.value[row][index]);
  891. }
  892. changed = true;
  893. } else {
  894. if (var_obj.type == Types.REAL) {
  895. parent_node.find('.span_value_variable').text(var_obj.value[row][index].toFixed(1));
  896. } else {
  897. parent_node.find('.span_value_variable').text(var_obj.value[row][index]);
  898. }
  899. }
  900. if (var_obj.type == Types.TEXT) {
  901. var_obj.value[row][index] = input_field.val();
  902. changed = true;
  903. parent_node.find('.span_value_variable').text(var_obj.value[row][index]);
  904. }
  905. if (changed) {
  906. registerUserEvent(function_name, ActionTypes.CHANGE_VAR_VALUE, var_obj.name,
  907. var_obj.value);
  908. }
  909. input_field.off();
  910. input_field.remove();
  911. /// update elements:
  912. opened_name_value_matrix_global_v = false;
  913. opened_input_value_matrix_global_v = false;
  914. }
  915. if(code == 27) {
  916. if (var_obj.type == Types.REAL) {
  917. parent_node.find('.span_value_variable').text(var_obj.value[row][index].toFixed(1));
  918. } else {
  919. parent_node.find('.span_value_variable').text(var_obj.value[row][index]);
  920. }
  921. input_field.off();
  922. input_field.remove();
  923. /// update elements:
  924. opened_name_value_matrix_global_v = false;
  925. opened_input_value_matrix_global_v = false;
  926. }
  927. });
  928. input_field.select();
  929. }