variables.js 34 KB

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