1
0

variables.js 28 KB

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