globals.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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> </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> </div> ';
  197. } else {
  198. ret += '<div class="created_div_valor_var"><span class="span_value_variable simple_var">'+global_var.value+'</span> </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. updateColumnsAndRowsText(global_container, global_var);
  308. }
  309. function addHandlers (global_container) {
  310. var global_var = global_container.data('associatedOject');
  311. // Manage constant option:
  312. global_container.find( ".alternate_constant" ).on('click', function(e){
  313. toggleConstant(global_var);
  314. $( this ).removeClass( "on off" );
  315. if (global_var.is_constant) {
  316. $( this ).addClass( "on" );
  317. } else {
  318. $( this ).addClass( "off" );
  319. }
  320. });
  321. // Manage global name:
  322. global_container.find( ".enable_edit_name_parameter" ).on('click', function(e){
  323. enableNameUpdate(global_container);
  324. });
  325. // Menu to change type:
  326. global_container.find('.ui.dropdown.global_type').dropdown({
  327. onChange: function(value, text, $selectedItem) {
  328. if ($($selectedItem).data('dimensions')) {
  329. updateType(global_var, Types[$($selectedItem).data('type')], $($selectedItem).data('dimensions'));
  330. } else {
  331. updateType(global_var, Types[$($selectedItem).data('type')]);
  332. }
  333. renderValues(global_var, global_container);
  334. }
  335. });
  336. // Remove global:
  337. global_container.find( ".remove_global" ).on('click', function(e){
  338. removeGlobal(global_var, global_container);
  339. });
  340. }
  341. function updateColumnsAndRowsText (global_container, global_var) {
  342. var prev = global_container.find('.text').text().split('[');
  343. if (prev.length == 2) {
  344. var ff = prev[0] + '[ ' + global_var.columns + ' ] ';
  345. global_container.find('.text').empty();
  346. global_container.find('.text').text(ff);
  347. }
  348. if (prev.length == 3) {
  349. var ff = prev[0] + '[ ' + global_var.columns + ' ] [ ' + global_var.rows + ' ] ';
  350. global_container.find('.text').empty();
  351. global_container.find('.text').text(ff);
  352. }
  353. }
  354. export function renderGlobal (global_var) {
  355. var element = '<div class="ui label global_container"><div class="global_const">const: ';
  356. element += '<i class="ui icon toggle '+(global_var.is_constant?"on":"off")+' alternate_constant"></i></div>';
  357. element += '<div class="ui dropdown global_type">';
  358. if (global_var.dimensions > 0) {
  359. element += '<div class="text">'+ i18n('ui:vector') + ':' + LocalizedStrings.getUI(global_var.type);
  360. for (var i = 0; i < global_var.dimensions; i ++) {
  361. element += ' [ <span class="dimensions_'+i+'"></span> ] ';
  362. }
  363. element += '</div>';
  364. } else {
  365. element += '<div class="text">' + LocalizedStrings.getUI(global_var.type.toLowerCase()) + '</div>';
  366. }
  367. element += '<div class="menu">';
  368. for (var tm in Types) {
  369. if (tm == Types.VOID.toUpperCase()) {
  370. continue;
  371. }
  372. element += '<div class="item ' + (global_var.type == tm.toLowerCase() ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(tm.toLowerCase())+'</div>';
  373. }
  374. for (var tm in Types) {
  375. if (tm == Types.VOID.toUpperCase()) {
  376. continue;
  377. }
  378. element += '<div class="item">'
  379. + '<i class="dropdown icon"></i>'
  380. + LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())
  381. + '<div class="menu">'
  382. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] " data-type="'+tm+'" data-dimensions="1">[ ]</div>'
  383. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] [ ] " data-type="'+tm+'" data-dimensions="2">[ ] [ ] </div>'
  384. + '</div>'
  385. + '</div>';
  386. }
  387. element += '</div></div> <div class="editing_name_var"> <span class="span_name_variable enable_edit_name_parameter">'+global_var.name+'</span> </div> <span class="character_equals"> = </span> ';
  388. element += '<div class="ui div_valor_var">'+global_var.value+'</div>';
  389. element += ' <i class="red icon times remove_global"></i></div>';
  390. var complete_element = $(element);
  391. complete_element.data('associatedOject', global_var);
  392. $('.list_globals').append(complete_element);
  393. addHandlers(complete_element);
  394. renderValues(global_var, complete_element);
  395. if (global_var.dimensions == 1) {
  396. complete_element.find('.dimensions_0').text(global_var.columns);
  397. }
  398. if (global_var.dimensions == 2) {
  399. complete_element.find('.dimensions_0').text(global_var.columns);
  400. complete_element.find('.dimensions_1').text(global_var.rows);
  401. }
  402. }
  403. var opened_name_value_matrix_global_v = false;
  404. var opened_input_value_matrix_global_v = null;
  405. function enableGlobalMatrixValueUpdate (global_var, row, index, parent_node) {
  406. if (opened_name_value_matrix_global_v) {
  407. $(opened_input_value_matrix_global_v).focus();
  408. return;
  409. }
  410. opened_name_value_matrix_global_v = true;
  411. $(parent_node).find('.span_value_variable').text('');
  412. if (global_var.type == Types.REAL) {
  413. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  414. + global_var.value[row][index].toFixed(1) + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  415. } else {
  416. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  417. + global_var.value[row][index] + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  418. }
  419. $('.width-dynamic').on('input', function() {
  420. var inputWidth = $(this).textWidth()+10;
  421. opened_input_value_matrix_global_v = this;
  422. $(this).focus();
  423. var tmpStr = $(this).val();
  424. $(this).val('');
  425. $(this).val(tmpStr);
  426. $(this).css({
  427. width: inputWidth
  428. })
  429. }).trigger('input');
  430. $('.width-dynamic').focusout(function() {
  431. /// update array:
  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. $('.width-dynamic').on('keydown', function(e) {
  451. var code = e.keyCode || e.which;
  452. if(code == 13) {
  453. if ($(this).val().trim()) {
  454. if (global_var.type == Types.REAL) {
  455. global_var.value[row][index] = parseFloat($(this).val().trim());
  456. $(parent_node).find('.span_value_variable').text(global_var.value[row][index].toFixed(1));
  457. } else {
  458. if (global_var.type == Types.INTEGER) {
  459. global_var.value[row][index] = parseInt($(this).val().trim());
  460. } else {
  461. global_var.value[row][index] = $(this).val().trim();
  462. }
  463. $(parent_node).find('.span_value_variable').text(global_var.value[row][index]);
  464. }
  465. }
  466. $(this).remove();
  467. /// update elements:
  468. opened_name_value_matrix_global_v = false;
  469. opened_input_value_matrix_global_v = false;
  470. }
  471. if(code == 27) {
  472. if (global_var.type == Types.REAL) {
  473. $(parent_node).find('.span_value_variable').text(global_var.value[row][index].toFixed(1));
  474. } else {
  475. $(parent_node).find('.span_value_variable').text(global_var.value[row][index]);
  476. }
  477. $(this).remove();
  478. /// update elements:
  479. opened_name_value_matrix_global_v = false;
  480. opened_input_value_matrix_global_v = false;
  481. }
  482. });
  483. }
  484. var opened_name_value_global_var = false;
  485. var opened_input_value_global_ar = null;
  486. function enableGlobalValueUpdate (global_var, parent_node) {
  487. if (opened_name_value_global_var) {
  488. $(opened_input_value_global_ar).focus();
  489. return;
  490. }
  491. opened_name_value_global_var = true;
  492. $(parent_node).find('.span_value_variable').text('');
  493. if (global_var.type == Types.REAL) {
  494. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  495. + global_var.value.toFixed(1) + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  496. } else {
  497. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  498. + global_var.value + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  499. }
  500. $('.width-dynamic').on('input', function() {
  501. var inputWidth = $(this).textWidth()+10;
  502. opened_input_value_global_ar = this;
  503. $(this).focus();
  504. var tmpStr = $(this).val();
  505. $(this).val('');
  506. $(this).val(tmpStr);
  507. $(this).css({
  508. width: inputWidth
  509. })
  510. }).trigger('input');
  511. $('.width-dynamic').focusout(function() {
  512. /// update array:
  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. $('.width-dynamic').on('keydown', function(e) {
  532. var code = e.keyCode || e.which;
  533. if(code == 13) {
  534. if ($(this).val().trim()) {
  535. if (global_var.type == Types.REAL) {
  536. global_var.value = parseFloat($(this).val().trim());
  537. $(parent_node).find('.span_value_variable').text(global_var.value.toFixed(1));
  538. } else{
  539. if (global_var.type == Types.INTEGER) {
  540. global_var.value = parseInt($(this).val().trim());
  541. } else {
  542. global_var.value = $(this).val().trim();
  543. }
  544. $(parent_node).find('.span_value_variable').text(global_var.value);
  545. }
  546. }
  547. $(this).remove();
  548. /// update elements:
  549. opened_name_value_global_var = false;
  550. opened_input_value_global_ar = false;
  551. }
  552. if(code == 27) {
  553. if (global_var.type == Types.REAL) {
  554. $(parent_node).find('.span_value_variable').text(global_var.value.toFixed(1));
  555. } else{
  556. $(parent_node).find('.span_value_variable').text(global_var.value);
  557. }
  558. $(this).remove();
  559. /// update elements:
  560. opened_name_value_global_var = false;
  561. opened_input_value_global_ar = false;
  562. }
  563. });
  564. }
  565. var opened_name_global = false;
  566. var opened_input_global = null;
  567. function enableNameUpdate (global_container) {
  568. var global_var = global_container.data('associatedOject');
  569. if (opened_name_global) {
  570. opened_input_global.focus();
  571. return;
  572. }
  573. opened_name_global = true;
  574. global_container.find('.span_name_variable').text('');
  575. $( "<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'));
  576. $('.width-dynamic').on('input', function() {
  577. var inputWidth = $(this).textWidth()+10;
  578. opened_input_global = $(this);
  579. opened_input_global.focus();
  580. opened_input_global.css({
  581. width: inputWidth
  582. })
  583. }).trigger('input');
  584. $('.width-dynamic').focusout(function() {
  585. /// update array:
  586. if ($(this).val().trim().length > 0) {
  587. updateName(global_var, $(this).val().trim());
  588. global_container.find('.span_name_variable').text(global_var.name);
  589. } else {
  590. global_container.find('.span_name_variable').text(global_var.name);
  591. }
  592. $(this).remove();
  593. /// update elements:
  594. opened_name_global = false;
  595. opened_input_global = false;
  596. });
  597. $('.width-dynamic').on('keydown', function(e) {
  598. var code = e.keyCode || e.which;
  599. if(code == 13) {
  600. if ($(this).val().trim()) {
  601. updateName(global_var, $(this).val().trim());
  602. global_container.find('.span_name_variable').text(global_var.name);
  603. } else {
  604. global_container.find('.span_name_variable').text(global_var.name);
  605. }
  606. $(this).remove();
  607. /// update elements:
  608. opened_name_global = false;
  609. opened_input_global = false;
  610. }
  611. if(code == 27) {
  612. global_container.find('.span_name_variable').text(global_var.name);
  613. $(this).remove();
  614. /// update elements:
  615. opened_name_global = false;
  616. opened_input_global = false;
  617. }
  618. });
  619. $('.width-dynamic').select();
  620. }
  621. var opened_name_value_vector_global_ = false;
  622. var opened_input_value_vector_global_ = null;
  623. function enableGlobalVectorValueUpdate (global_var, index, parent_node) {
  624. if (opened_name_value_vector_global_) {
  625. $(opened_input_value_vector_global_).focus();
  626. return;
  627. }
  628. opened_name_value_vector_global_ = true;
  629. $(parent_node).find('.span_value_variable').text('');
  630. if (global_var.type == Types.REAL) {
  631. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  632. + global_var.value[index].toFixed(1) + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  633. } else {
  634. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  635. + global_var.value[index] + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  636. }
  637. $('.width-dynamic').on('input', function() {
  638. var inputWidth = $(this).textWidth()+10;
  639. opened_input_value_vector_global_ = this;
  640. $(this).focus();
  641. var tmpStr = $(this).val();
  642. $(this).val('');
  643. $(this).val(tmpStr);
  644. $(this).css({
  645. width: inputWidth
  646. })
  647. }).trigger('input');
  648. $('.width-dynamic').focusout(function() {
  649. /// update array:
  650. if ($(this).val().trim()) {
  651. if (global_var.type == Types.REAL) {
  652. global_var.value[index] = parseFloat($(this).val().trim());
  653. $(parent_node).find('.span_value_variable').text(global_var.value[index].toFixed(1));
  654. } else {
  655. if (global_var.type == Types.INTEGER) {
  656. global_var.value[index] = parseInt($(this).val().trim());
  657. } else {
  658. global_var.value[index] = $(this).val().trim();
  659. }
  660. $(parent_node).find('.span_value_variable').text(global_var.value[index]);
  661. }
  662. }
  663. $(this).remove();
  664. /// update elements:
  665. opened_name_value_vector_global_ = false;
  666. opened_input_value_vector_global_ = false;
  667. });
  668. $('.width-dynamic').on('keydown', function(e) {
  669. var code = e.keyCode || e.which;
  670. if(code == 13) {
  671. if ($(this).val().trim()) {
  672. if (global_var.type == Types.REAL) {
  673. global_var.value[index] = parseFloat($(this).val().trim());
  674. $(parent_node).find('.span_value_variable').text(global_var.value[index].toFixed(1));
  675. } else {
  676. if (global_var.type == Types.INTEGER) {
  677. global_var.value[index] = parseInt($(this).val().trim());
  678. } else {
  679. global_var.value[index] = $(this).val().trim();
  680. }
  681. $(parent_node).find('.span_value_variable').text(global_var.value[index]);
  682. }
  683. }
  684. $(this).remove();
  685. /// update elements:
  686. opened_name_value_vector_global_ = false;
  687. opened_input_value_vector_global_ = false;
  688. }
  689. if(code == 27) {
  690. if (global_var.type == Types.REAL) {
  691. $(parent_node).find('.span_value_variable').text(global_var.value[index].toFixed(1));
  692. } else {
  693. $(parent_node).find('.span_value_variable').text(global_var.value[index]);
  694. }
  695. $(this).remove();
  696. /// update elements:
  697. opened_name_value_vector_global_ = false;
  698. opened_input_value_vector_global_ = false;
  699. }
  700. });
  701. }
  702. $.fn.textWidth = function(text, font) {
  703. if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
  704. $.fn.textWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));
  705. return $.fn.textWidth.fakeEl.width();
  706. };