variables.js 28 KB

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