globals.js 30 KB

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