globals.js 34 KB

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