variables.js 29 KB

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