1
0

variables.js 30 KB

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