variables.js 34 KB

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