globals.js 30 KB

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