variables.js 30 KB

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