globals.js 30 KB

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