globals.js 30 KB

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