globals.js 37 KB

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