globals.js 36 KB

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