globals.js 37 KB

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