globals.js 36 KB

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