variables.js 36 KB

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