globals.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. import $ from 'jquery';
  2. import jQuery from 'jquery';
  3. import { Types } from './types';
  4. import * as Models from './ivprog_elements';
  5. import { LocalizedStrings } from './../services/localizedStringsService';
  6. window.jQuery = jQuery;
  7. import '../semantic/semantic.min.js';
  8. var counter_new_globals = 0;
  9. export function addGlobal (program) {
  10. var new_global = new Models.Variable(Types.INTEGER, LocalizedStrings.getUI('new_global') + '_' + counter_new_globals, 1);
  11. counter_new_globals ++;
  12. program.addGlobal(new_global);
  13. renderGlobal(new_global);
  14. }
  15. function toggleConstant (global_var) {
  16. global_var.is_constant = !global_var.is_constant;
  17. }
  18. function updateName (global_var, new_name) {
  19. global_var.name = new_name;
  20. }
  21. function updateType (global_var, new_type, new_dimensions = 0) {
  22. global_var.type = new_type;
  23. global_var.dimensions = new_dimensions;
  24. if (new_dimensions > 0) {
  25. global_var.rows = new_dimensions;
  26. global_var.columns = 2;
  27. }
  28. updateInitialValues(global_var);
  29. }
  30. function removeGlobal (global_var, global_container) {
  31. var index = window.program_obj.globals.indexOf(global_var);
  32. if (index > -1) {
  33. window.program_obj.globals.splice(index, 1);
  34. }
  35. $(global_container).remove();
  36. }
  37. function updateInitialValues (global_var) {
  38. if (global_var.type == Types.INTEGER) {
  39. if (global_var.dimensions == 0) {
  40. global_var.value = 1;
  41. }
  42. if (global_var.dimensions == 1) {
  43. global_var.value = [1, 1];
  44. }
  45. if (global_var.dimensions == 2) {
  46. global_var.value = [[1, 1], [1, 1]];
  47. }
  48. }
  49. if (global_var.type == Types.REAL) {
  50. if (global_var.dimensions == 0) {
  51. global_var.value = 1.0;
  52. }
  53. if (global_var.dimensions == 1) {
  54. global_var.value = [1.0, 1.0];
  55. }
  56. if (global_var.dimensions == 2) {
  57. global_var.value = [[1.0, 1.0], [1.0, 1.0]];
  58. }
  59. }
  60. if (global_var.type == Types.TEXT) {
  61. if (global_var.dimensions == 0) {
  62. global_var.value = LocalizedStrings.getUI('text_start');
  63. }
  64. if (global_var.dimensions == 1) {
  65. global_var.value = [LocalizedStrings.getUI('text_start'), LocalizedStrings.getUI('text_start')];
  66. }
  67. if (global_var.dimensions == 2) {
  68. global_var.value = [[LocalizedStrings.getUI('text_start'), LocalizedStrings.getUI('text_start')],
  69. [LocalizedStrings.getUI('text_start'), LocalizedStrings.getUI('text_start')]];
  70. }
  71. }
  72. if (global_var.type == Types.BOOLEAN) {
  73. if (global_var.dimensions == 0) {
  74. global_var.value = true;
  75. }
  76. if (global_var.dimensions == 1) {
  77. global_var.value = [true, true];
  78. }
  79. if (global_var.dimensions == 2) {
  80. global_var.value = [[true, true], [true, true]];
  81. }
  82. }
  83. }
  84. function alternateBooleanGlobalValue (global_var, value_container) {
  85. global_var.value = !global_var.value;
  86. $(value_container).find('.span_value_variable').text(global_var.value);
  87. }
  88. function alternateBooleanGlobalVectorValue (global_var, index, value_container) {
  89. global_var.value[index] = !global_var.value[index];
  90. $(value_container).find('.span_value_variable').text(global_var.value[index]);
  91. }
  92. function removeGlobalColumnVector (global_var) {
  93. if (global_var.columns == 0) {
  94. return;
  95. }
  96. global_var.columns --;
  97. global_var.value.splice(global_var.value.length - 1, 1);
  98. }
  99. function addGlobalColumnVector (global_var) {
  100. global_var.columns ++;
  101. if (global_var.type == Types.INTEGER) {
  102. global_var.value.push(1);
  103. }
  104. if (global_var.type == Types.REAL) {
  105. global_var.value.push(1.0);
  106. }
  107. if (global_var.type == Types.TEXT) {
  108. global_var.value.push(LocalizedStrings.getUI('text_start'));
  109. }
  110. if (global_var.type == Types.BOOLEAN) {
  111. global_var.value.push(true);
  112. }
  113. }
  114. function removeColumnGlobalMatrix (global_var) {
  115. if (global_var.columns == 0) {
  116. return;
  117. }
  118. global_var.columns --;
  119. for (var i = 0; i < global_var.rows; i++) {
  120. global_var.value[i].splice(global_var.value[i].length - 1, 1);
  121. }
  122. }
  123. function addColumnGlobalMatrix (global_var) {
  124. global_var.columns ++;
  125. if (global_var.type == Types.INTEGER) {
  126. for (var i = 0; i < global_var.rows; i++) {
  127. global_var.value[i].push(1);
  128. }
  129. }
  130. if (global_var.type == Types.REAL) {
  131. for (var i = 0; i < global_var.rows; i++) {
  132. global_var.value[i].push(1.0);
  133. }
  134. }
  135. if (global_var.type == Types.TEXT) {
  136. for (var i = 0; i < global_var.rows; i++) {
  137. global_var.value[i].push(LocalizedStrings.getUI('text_start'));
  138. }
  139. }
  140. if (global_var.type == Types.BOOLEAN) {
  141. for (var i = 0; i < global_var.rows; i++) {
  142. global_var.value[i].push(true);
  143. }
  144. }
  145. }
  146. function removeLineGlobalMatrix (global_var) {
  147. if (global_var.rows == 0) {
  148. return;
  149. }
  150. global_var.rows --;
  151. global_var.value.splice(global_var.value.length - 1, 1);
  152. }
  153. function addLineGlobalMatrix (global_var) {
  154. global_var.rows ++;
  155. if (global_var.type == Types.INTEGER) {
  156. var n_l = [];
  157. for (var i = 0; i < global_var.columns; i++) {
  158. n_l.push(1);
  159. }
  160. global_var.value.push(n_l);
  161. }
  162. if (global_var.type == Types.REAL) {
  163. var n_l = [];
  164. for (i = 0; i < global_var.columns; i++) {
  165. n_l.push(1.0);
  166. }
  167. global_var.value.push(n_l);
  168. }
  169. if (global_var.type == Types.TEXT) {
  170. var n_l = [];
  171. for (i = 0; i < global_var.columns; i++) {
  172. n_l.push(LocalizedStrings.getUI('text_start'));
  173. }
  174. global_var.value.push(n_l);
  175. }
  176. if (global_var.type == Types.BOOLEAN) {
  177. var n_l = [];
  178. for (i = 0; i < global_var.columns; i++) {
  179. n_l.push(true);
  180. }
  181. global_var.value.push(n_l);
  182. }
  183. }
  184. function alternateBooleanGlobalMatrixValue (global_var, row, index, value_container) {
  185. global_var.value[row][index] = !global_var.value[row][index];
  186. $(value_container).find('.span_value_variable').text(global_var.value[row][index]);
  187. }
  188. function renderValues (global_var, global_container) {
  189. var ret = "";
  190. var j = 0;
  191. if (global_var.dimensions == 0) {
  192. if (global_var.type == Types.REAL) {
  193. ret += '<div class="created_div_valor_var"><span class="span_value_variable simple_var">'+global_var.value.toFixed(1)+'</span> <i class="icon small pencil alternate enable_edit_name_function simple_var"></i></div> ';
  194. } else {
  195. if (global_var.type == Types.BOOLEAN) {
  196. ret += '<div class="created_div_valor_var"><span class="span_value_variable boolean_simple_type">'+global_var.value+'</span> <i class="icon small pencil alternate enable_edit_name_function boolean_simple_type"></i></div> ';
  197. } else {
  198. ret += '<div class="created_div_valor_var"><span class="span_value_variable simple_var">'+global_var.value+'</span> <i class="icon small pencil alternate enable_edit_name_function simple_var"></i></div> ';
  199. }
  200. }
  201. } else {
  202. ret += '<table class="tabela_var">';
  203. if (global_var.dimensions == 1) {
  204. ret += '<tr>';
  205. if (global_var.type == Types.REAL) {
  206. for (var k = 0; k < global_var.columns; k++) {
  207. ret += '<td><span class="span_value_variable vector_var" data-index="'+k+'">'+global_var.value[k].toFixed(1)+'</span></td>';
  208. }
  209. } else {
  210. for (var k = 0; k < global_var.columns; k++) {
  211. if (global_var.type == Types.BOOLEAN) {
  212. ret += '<td><span class="span_value_variable boolean_vector_var" data-index="'+k+'">'+global_var.value[k]+'</span></td>';
  213. } else {
  214. ret += '<td><span class="span_value_variable vector_var" data-index="'+k+'">'+global_var.value[k]+'</span>'+'</td>';
  215. }
  216. }
  217. }
  218. ret += '</tr>';
  219. ret += '</table>';
  220. ret += '<div class="buttons_manage_columns"><i class="ui icon minus square outline remove_global_vector_column"></i>'
  221. + ' <i class="ui icon plus square outline add_global_vector_column"></i></div>';
  222. }
  223. if (global_var.dimensions == 2) {
  224. if (global_var.type == Types.REAL) {
  225. for (var l = 0; l < global_var.rows; l++) {
  226. ret += '<tr>';
  227. for (var k = 0; k < global_var.columns; k++) {
  228. ret += '<td><span class="span_value_variable matrix_var" data-index="'+k+'" data-row="'+l+'">'+global_var.value[l][k].toFixed(1)+'</span>'+'</td>';
  229. }
  230. ret += '</tr>';
  231. }
  232. } else {
  233. for (var l = 0; l < global_var.rows; l++) {
  234. ret += '<tr>';
  235. for (var k = 0; k < global_var.columns; k++) {
  236. if (global_var.type == Types.BOOLEAN) {
  237. ret += '<td><span class="span_value_variable boolean_matrix_var" data-index="'+k+'" data-row="'+l+'">'+global_var.value[l][k]+'</span></td>';
  238. } else {
  239. ret += '<td><span class="span_value_variable matrix_var" data-index="'+k+'" data-row="'+l+'">'+global_var.value[l][k]+'</span></td>';
  240. }
  241. }
  242. ret += '</tr>';
  243. }
  244. }
  245. if (global_var.rows == 0) {
  246. ret += '<tr><td></td></tr>';
  247. }
  248. ret += '<tr><td colspan="'+global_var.columns+'" class="tr_manage_lines"><i class="ui icon minus square outline remove_global_matrix_line"></i>'
  249. + ' <i class="ui icon plus square outline add_global_matrix_line"></i></td></tr>';
  250. ret += '</table>';
  251. ret += '<div class="buttons_manage_columns"><i class="ui icon minus square outline remove_global_matrix_column"></i>'
  252. + ' <i class="ui icon plus square outline add_global_matrix_column"></i></div>';
  253. }
  254. }
  255. $( global_container ).find( ".div_valor_var" ).html('');
  256. ret = $(ret);
  257. $(ret).find('.span_value_variable').data('associatedOject', global_var);
  258. $( ret ).find( ".boolean_simple_type" ).on('click', function(e){
  259. alternateBooleanGlobalValue(global_var, this.parentNode);
  260. });
  261. $( ret ).find( ".simple_var" ).on('click', function(e){
  262. enableGlobalValueUpdate(global_var, this.parentNode);
  263. });
  264. $( ret ).find( ".boolean_vector_var" ).on('click', function(e){
  265. alternateBooleanGlobalVectorValue(global_var, $(this).data('index'), this.parentNode);
  266. });
  267. $( ret ).find( ".vector_var" ).on('click', function(e){
  268. enableGlobalVectorValueUpdate(global_var, $(this).data('index'), this.parentNode);
  269. });
  270. $( ret ).find( ".remove_global_vector_column" ).on('click', function(e){
  271. removeGlobalColumnVector(global_var);
  272. $( global_container ).find( ".div_valor_var" ).html('');
  273. renderValues(global_var, global_container);
  274. });
  275. $( ret ).find( ".add_global_vector_column" ).on('click', function(e){
  276. addGlobalColumnVector(global_var);
  277. $( global_container ).find( ".div_valor_var" ).html('');
  278. renderValues(global_var, global_container);
  279. });
  280. $( ret ).find( ".remove_global_matrix_column" ).on('click', function(e){
  281. removeColumnGlobalMatrix(global_var);
  282. $( global_container ).find( ".div_valor_var" ).html('');
  283. renderValues(global_var, global_container);
  284. });
  285. $( ret ).find( ".add_global_matrix_column" ).on('click', function(e){
  286. addColumnGlobalMatrix(global_var);
  287. $( global_container ).find( ".div_valor_var" ).html('');
  288. renderValues(global_var, global_container);
  289. });
  290. $( ret ).find( ".remove_global_matrix_line" ).on('click', function(e){
  291. removeLineGlobalMatrix(global_var);
  292. $( global_container ).find( ".div_valor_var" ).html('');
  293. renderValues(global_var, global_container);
  294. });
  295. $( ret ).find( ".add_global_matrix_line" ).on('click', function(e){
  296. addLineGlobalMatrix(global_var);
  297. $( global_container ).find( ".div_valor_var" ).html('');
  298. renderValues(global_var, global_container);
  299. });
  300. $( ret ).find( ".boolean_matrix_var" ).on('click', function(e){
  301. alternateBooleanGlobalMatrixValue(global_var, $(this).data('row'), $(this).data('index'), this.parentNode);
  302. });
  303. $( ret ).find( ".matrix_var" ).on('click', function(e){
  304. enableGlobalMatrixValueUpdate(global_var, $(this).data('row'), $(this).data('index'), this.parentNode);
  305. });
  306. $( global_container ).find( ".div_valor_var" ).append(ret);
  307. }
  308. function addHandlers (global_container) {
  309. var global_var = $(global_container).data('associatedOject');
  310. // Manage constant option:
  311. $( global_container ).find( ".alternate_constant" ).on('click', function(e){
  312. toggleConstant(global_var);
  313. $( this ).removeClass( "on off" );
  314. if (global_var.is_constant) {
  315. $( this ).addClass( "on" );
  316. } else {
  317. $( this ).addClass( "off" );
  318. }
  319. });
  320. // Manage global name:
  321. $( global_container ).find( ".enable_edit_name_parameter" ).on('click', function(e){
  322. enableNameUpdate(global_container);
  323. });
  324. // Menu to change type:
  325. $( global_container ).find('.ui.dropdown.global_type').dropdown({
  326. onChange: function(value, text, $selectedItem) {
  327. if ($($selectedItem).data('dimensions')) {
  328. updateType(global_var, Types[$($selectedItem).data('type')], $($selectedItem).data('dimensions'));
  329. } else {
  330. updateType(global_var, Types[$($selectedItem).data('type')]);
  331. }
  332. renderValues(global_var, global_container);
  333. }
  334. });
  335. // Remove global:
  336. $( global_container ).find( ".remove_global" ).on('click', function(e){
  337. removeGlobal(global_var, global_container);
  338. });
  339. }
  340. function renderGlobal (global_var) {
  341. var element = '<div class="ui label global_container"><div class="global_const">const: ';
  342. element += '<i class="ui icon toggle '+(global_var.is_constant?"on":"off")+' alternate_constant"></i></div><span class="span_name_variable enable_edit_name_parameter">'+global_var.name+'</span> <i class="icon small pencil alternate enable_edit_name_parameter"></i>';
  343. element += '<div class="ui dropdown global_type">';
  344. if (global_var.dimensions > 0) {
  345. element += '<div class="text">'+ i18n('ui:vector') + ':' + LocalizedStrings.getUI(global_var.type);
  346. for (var i = 0; i < global_var.dimensions; i ++) {
  347. element += ' [ ] ';
  348. }
  349. element += '</div>';
  350. } else {
  351. element += '<div class="text">' + LocalizedStrings.getUI(global_var.type.toLowerCase()) + '</div>';
  352. }
  353. element += '<i class="dropdown icon"></i><div class="menu">';
  354. for (var tm in Types) {
  355. if (tm == Types.VOID.toUpperCase()) {
  356. continue;
  357. }
  358. element += '<div class="item ' + (global_var.type == tm.toLowerCase() ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(tm.toLowerCase())+'</div>';
  359. }
  360. for (var tm in Types) {
  361. if (tm == Types.VOID.toUpperCase()) {
  362. continue;
  363. }
  364. element += '<div class="item">'
  365. + '<i class="dropdown icon"></i>'
  366. + LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())
  367. + '<div class="menu">'
  368. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] " data-type="'+tm+'" data-dimensions="1">[ ]</div>'
  369. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] [ ] " data-type="'+tm+'" data-dimensions="2">[ ] [ ] </div>'
  370. + '</div>'
  371. + '</div>';
  372. }
  373. element += '</div></div> = ';
  374. element += '<div class="ui div_valor_var">'+global_var.value+'</div>';
  375. element += ' <i class="red icon times remove_global"></i></div>';
  376. var complete_element = $(element);
  377. $(complete_element).data('associatedOject', global_var);
  378. $('.list_globals').append(complete_element);
  379. addHandlers(complete_element);
  380. renderValues(global_var, complete_element);
  381. }
  382. var opened_name_value_matrix_global_v = false;
  383. var opened_input_value_matrix_global_v = null;
  384. function enableGlobalMatrixValueUpdate (global_var, row, index, parent_node) {
  385. if (opened_name_value_matrix_global_v) {
  386. $(opened_input_value_matrix_global_v).focus();
  387. return;
  388. }
  389. opened_name_value_matrix_global_v = true;
  390. $(parent_node).find('.span_value_variable').text('');
  391. if (global_var.type == Types.REAL) {
  392. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  393. + global_var.value[row][index].toFixed(1) + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  394. } else {
  395. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  396. + global_var.value[row][index] + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  397. }
  398. $('.width-dynamic').on('input', function() {
  399. var inputWidth = $(this).textWidth()+10;
  400. opened_input_value_matrix_global_v = this;
  401. $(this).focus();
  402. var tmpStr = $(this).val();
  403. $(this).val('');
  404. $(this).val(tmpStr);
  405. $(this).css({
  406. width: inputWidth
  407. })
  408. }).trigger('input');
  409. $('.width-dynamic').focusout(function() {
  410. /// update array:
  411. if ($(this).val().trim()) {
  412. if (global_var.type == Types.REAL) {
  413. global_var.value[row][index] = parseFloat($(this).val().trim());
  414. $(parent_node).find('.span_value_variable').text(global_var.value[row][index].toFixed(1));
  415. } else {
  416. if (global_var.type == Types.INTEGER) {
  417. global_var.value[row][index] = parseInt($(this).val().trim());
  418. } else {
  419. global_var.value[row][index] = $(this).val().trim();
  420. }
  421. $(parent_node).find('.span_value_variable').text(global_var.value[row][index]);
  422. }
  423. }
  424. $(this).remove();
  425. /// update elements:
  426. opened_name_value_matrix_global_v = false;
  427. opened_input_value_matrix_global_v = false;
  428. });
  429. $('.width-dynamic').on('keydown', function(e) {
  430. var code = e.keyCode || e.which;
  431. if(code == 13) {
  432. if ($(this).val().trim()) {
  433. if (global_var.type == Types.REAL) {
  434. global_var.value[row][index] = parseFloat($(this).val().trim());
  435. $(parent_node).find('.span_value_variable').text(global_var.value[row][index].toFixed(1));
  436. } else {
  437. if (global_var.type == Types.INTEGER) {
  438. global_var.value[row][index] = parseInt($(this).val().trim());
  439. } else {
  440. global_var.value[row][index] = $(this).val().trim();
  441. }
  442. $(parent_node).find('.span_value_variable').text(global_var.value[row][index]);
  443. }
  444. }
  445. $(this).remove();
  446. /// update elements:
  447. opened_name_value_matrix_global_v = false;
  448. opened_input_value_matrix_global_v = false;
  449. }
  450. if(code == 27) {
  451. if (global_var.type == Types.REAL) {
  452. $(parent_node).find('.span_value_variable').text(global_var.value[row][index].toFixed(1));
  453. } else {
  454. $(parent_node).find('.span_value_variable').text(global_var.value[row][index]);
  455. }
  456. $(this).remove();
  457. /// update elements:
  458. opened_name_value_matrix_global_v = false;
  459. opened_input_value_matrix_global_v = false;
  460. }
  461. });
  462. }
  463. var opened_name_value_global_var = false;
  464. var opened_input_value_global_ar = null;
  465. function enableGlobalValueUpdate (global_var, parent_node) {
  466. if (opened_name_value_global_var) {
  467. $(opened_input_value_global_ar).focus();
  468. return;
  469. }
  470. opened_name_value_global_var = true;
  471. $(parent_node).find('.span_value_variable').text('');
  472. if (global_var.type == Types.REAL) {
  473. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  474. + global_var.value.toFixed(1) + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  475. } else {
  476. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  477. + global_var.value + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  478. }
  479. $('.width-dynamic').on('input', function() {
  480. var inputWidth = $(this).textWidth()+10;
  481. opened_input_value_global_ar = this;
  482. $(this).focus();
  483. var tmpStr = $(this).val();
  484. $(this).val('');
  485. $(this).val(tmpStr);
  486. $(this).css({
  487. width: inputWidth
  488. })
  489. }).trigger('input');
  490. $('.width-dynamic').focusout(function() {
  491. /// update array:
  492. if ($(this).val().trim()) {
  493. if (global_var.type == Types.REAL) {
  494. global_var.value = parseFloat($(this).val().trim());
  495. $(parent_node).find('.span_value_variable').text(global_var.value.toFixed(1));
  496. } else{
  497. if (global_var.type == Types.INTEGER) {
  498. global_var.value = parseInt($(this).val().trim());
  499. } else {
  500. global_var.value = $(this).val().trim();
  501. }
  502. $(parent_node).find('.span_value_variable').text(global_var.value);
  503. }
  504. }
  505. $(this).remove();
  506. /// update elements:
  507. opened_name_value_global_var = false;
  508. opened_input_value_global_ar = false;
  509. });
  510. $('.width-dynamic').on('keydown', function(e) {
  511. var code = e.keyCode || e.which;
  512. if(code == 13) {
  513. if ($(this).val().trim()) {
  514. if (global_var.type == Types.REAL) {
  515. global_var.value = parseFloat($(this).val().trim());
  516. $(parent_node).find('.span_value_variable').text(global_var.value.toFixed(1));
  517. } else{
  518. if (global_var.type == Types.INTEGER) {
  519. global_var.value = parseInt($(this).val().trim());
  520. } else {
  521. global_var.value = $(this).val().trim();
  522. }
  523. $(parent_node).find('.span_value_variable').text(global_var.value);
  524. }
  525. }
  526. $(this).remove();
  527. /// update elements:
  528. opened_name_value_global_var = false;
  529. opened_input_value_global_ar = false;
  530. }
  531. if(code == 27) {
  532. if (global_var.type == Types.REAL) {
  533. $(parent_node).find('.span_value_variable').text(global_var.value.toFixed(1));
  534. } else{
  535. $(parent_node).find('.span_value_variable').text(global_var.value);
  536. }
  537. $(this).remove();
  538. /// update elements:
  539. opened_name_value_global_var = false;
  540. opened_input_value_global_ar = false;
  541. }
  542. });
  543. }
  544. var opened_name_global = false;
  545. var opened_input_global = null;
  546. function enableNameUpdate (global_container) {
  547. var global_var = $(global_container).data('associatedOject');
  548. if (opened_name_global) {
  549. $(opened_input_global).focus();
  550. return;
  551. }
  552. opened_name_global = true;
  553. $( global_container ).find('.span_name_variable').text('');
  554. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+global_var.name+"' />" ).insertBefore($(global_container).find('.span_name_variable'));
  555. $('.width-dynamic').on('input', function() {
  556. var inputWidth = $(this).textWidth()+10;
  557. opened_input_global = this;
  558. $(this).focus();
  559. var tmpStr = $(this).val();
  560. $(this).val('');
  561. $(this).val(tmpStr);
  562. $(this).css({
  563. width: inputWidth
  564. })
  565. }).trigger('input');
  566. $('.width-dynamic').focusout(function() {
  567. /// update array:
  568. if ($(this).val().trim()) {
  569. updateName(global_var, $(this).val().trim());
  570. $(global_container).find('.span_name_variable').text(global_var.name);
  571. }
  572. $(this).remove();
  573. /// update elements:
  574. opened_name_global = false;
  575. opened_input_global = false;
  576. });
  577. $('.width-dynamic').on('keydown', function(e) {
  578. var code = e.keyCode || e.which;
  579. if(code == 13) {
  580. if ($(this).val().trim()) {
  581. updateName(global_var, $(this).val().trim());
  582. $(global_container).find('.span_name_variable').text(global_var.name);
  583. }
  584. $(this).remove();
  585. /// update elements:
  586. opened_name_global = false;
  587. opened_input_global = false;
  588. }
  589. if(code == 27) {
  590. $(global_container).find('.span_name_variable').text(global_var.name);
  591. $(this).remove();
  592. /// update elements:
  593. opened_name_global = false;
  594. opened_input_global = false;
  595. }
  596. });
  597. }
  598. var opened_name_value_vector_global_ = false;
  599. var opened_input_value_vector_global_ = null;
  600. function enableGlobalVectorValueUpdate (global_var, index, parent_node) {
  601. if (opened_name_value_vector_global_) {
  602. $(opened_input_value_vector_global_).focus();
  603. return;
  604. }
  605. opened_name_value_vector_global_ = true;
  606. $(parent_node).find('.span_value_variable').text('');
  607. if (global_var.type == Types.REAL) {
  608. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  609. + global_var.value[index].toFixed(1) + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  610. } else {
  611. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  612. + global_var.value[index] + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  613. }
  614. $('.width-dynamic').on('input', function() {
  615. var inputWidth = $(this).textWidth()+10;
  616. opened_input_value_vector_global_ = this;
  617. $(this).focus();
  618. var tmpStr = $(this).val();
  619. $(this).val('');
  620. $(this).val(tmpStr);
  621. $(this).css({
  622. width: inputWidth
  623. })
  624. }).trigger('input');
  625. $('.width-dynamic').focusout(function() {
  626. /// update array:
  627. if ($(this).val().trim()) {
  628. if (global_var.type == Types.REAL) {
  629. global_var.value[index] = parseFloat($(this).val().trim());
  630. $(parent_node).find('.span_value_variable').text(global_var.value[index].toFixed(1));
  631. } else {
  632. if (global_var.type == Types.INTEGER) {
  633. global_var.value[index] = parseInt($(this).val().trim());
  634. } else {
  635. global_var.value[index] = $(this).val().trim();
  636. }
  637. $(parent_node).find('.span_value_variable').text(global_var.value[index]);
  638. }
  639. }
  640. $(this).remove();
  641. /// update elements:
  642. opened_name_value_vector_global_ = false;
  643. opened_input_value_vector_global_ = false;
  644. });
  645. $('.width-dynamic').on('keydown', function(e) {
  646. var code = e.keyCode || e.which;
  647. if(code == 13) {
  648. if ($(this).val().trim()) {
  649. if (global_var.type == Types.REAL) {
  650. global_var.value[index] = parseFloat($(this).val().trim());
  651. $(parent_node).find('.span_value_variable').text(global_var.value[index].toFixed(1));
  652. } else {
  653. if (global_var.type == Types.INTEGER) {
  654. global_var.value[index] = parseInt($(this).val().trim());
  655. } else {
  656. global_var.value[index] = $(this).val().trim();
  657. }
  658. $(parent_node).find('.span_value_variable').text(global_var.value[index]);
  659. }
  660. }
  661. $(this).remove();
  662. /// update elements:
  663. opened_name_value_vector_global_ = false;
  664. opened_input_value_vector_global_ = false;
  665. }
  666. if(code == 27) {
  667. if (global_var.type == Types.REAL) {
  668. $(parent_node).find('.span_value_variable').text(global_var.value[index].toFixed(1));
  669. } else {
  670. $(parent_node).find('.span_value_variable').text(global_var.value[index]);
  671. }
  672. $(this).remove();
  673. /// update elements:
  674. opened_name_value_vector_global_ = false;
  675. opened_input_value_vector_global_ = false;
  676. }
  677. });
  678. }
  679. $.fn.textWidth = function(text, font) {
  680. if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
  681. $.fn.textWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));
  682. return $.fn.textWidth.fakeEl.width();
  683. };