globals.js 30 KB

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