variables.js 36 KB

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