variables.js 29 KB

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