globals.js 30 KB

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