globals.js 29 KB

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