globals.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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. 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(`logic_value_${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(`logic_value_${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(0);
  138. }
  139. if (global_var.type == Types.REAL) {
  140. global_var.value.push(0.0);
  141. }
  142. if (global_var.type == Types.TEXT) {
  143. global_var.value.push(LocalizedStrings.getUI('textvar_default_value'));
  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(0);
  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(0.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('textvar_default_value'));
  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(0);
  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(0.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('textvar_default_value'));
  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(`logic_value_${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(`logic_value_${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(`logic_value_${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(`logic_value_${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( ".editing_name_var" ).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 == 1) {
  420. element += '<div class="text">'+ LocalizedStrings.getUI('vector')+ ': ' + LocalizedStrings.getUI(`type_${global_var.type}`);
  421. element += ' [ <span class="dimensions_0"></span> ] ';
  422. element += '</div>';
  423. } else if (global_var.dimensions == 2) {
  424. element += '<div class="text">'+ LocalizedStrings.getUI('matrix')+ ': ' + LocalizedStrings.getUI(`type_${global_var.type}`);
  425. element += ' [ <span class="dimensions_0"></span> ] [ <span class="dimensions_1"></span> ] ';
  426. element += '</div>';
  427. } else {
  428. element += '<div class="text">' + LocalizedStrings.getUI(`type_${global_var.type.toLowerCase()}`) + '</div>';
  429. }
  430. element += '<div class="menu">';
  431. for (var tm in Types) {
  432. if (tm == Types.VOID.toUpperCase()) {
  433. continue;
  434. }
  435. element += '<div class="item ' + (global_var.type == tm.toLowerCase() && global_var.dimensions == 0 ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(`type_${tm.toLowerCase()}`)+'</div>';
  436. }
  437. element += '<div class="item ' + (global_var.dimensions == 1 ? ' selected ' : '') + '">'
  438. + '<i class="dropdown icon"></i>' + LocalizedStrings.getUI('vector')
  439. + '<div class="menu">';
  440. for (var tm in Types) {
  441. if (tm == Types.VOID.toUpperCase()) {
  442. continue;
  443. }
  444. 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>';
  445. }
  446. element += '</div></div>';
  447. element += '<div class="item ' + (global_var.dimensions == 2 ? ' selected ' : '') + '">'
  448. + '<i class="dropdown icon"></i>' + LocalizedStrings.getUI('matrix')
  449. + '<div class="menu">';
  450. for (var tm in Types) {
  451. if (tm == Types.VOID.toUpperCase()) {
  452. continue;
  453. }
  454. 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>';
  455. }
  456. element += '</div></div>';
  457. 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> ';
  458. element += '<div class="ui div_valor_var">'+global_var.value+'</div>';
  459. element += ' <i class="yellow inverted icon times remove_global"></i></div>';
  460. var complete_element = $(element);
  461. complete_element.data('associatedOject', global_var);
  462. $('.list_globals').append(complete_element);
  463. addHandlers(complete_element);
  464. renderValues(global_var, complete_element);
  465. if (global_var.dimensions == 1) {
  466. complete_element.find('.dimensions_0').text(global_var.columns);
  467. }
  468. if (global_var.dimensions == 2) {
  469. complete_element.find('.dimensions_0').text(global_var.columns);
  470. complete_element.find('.dimensions_1').text(global_var.rows);
  471. }
  472. return complete_element;
  473. }
  474. var opened_name_value_matrix_global_v = false;
  475. var opened_input_value_matrix_global_v = null;
  476. function enableGlobalMatrixValueUpdate (global_var, row, index, parent_node) {
  477. if (opened_name_value_matrix_global_v) {
  478. opened_input_value_matrix_global_v.focus();
  479. return;
  480. }
  481. parent_node = $(parent_node);
  482. opened_name_value_matrix_global_v = true;
  483. parent_node.find('.span_value_variable').text('');
  484. var input_field;
  485. if (global_var.type == Types.REAL) {
  486. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  487. + global_var.value[row][index].toFixed(1) + "' />" );
  488. input_field.insertBefore(parent_node.find('.span_value_variable'));
  489. } else {
  490. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  491. + global_var.value[row][index] + "' />" );
  492. input_field.insertBefore(parent_node.find('.span_value_variable'));
  493. }
  494. input_field.on('input', function() {
  495. var inputWidth = input_field.textWidth()+10;
  496. opened_input_value_matrix_global_v = input_field;
  497. input_field.focus();
  498. var tmpStr = input_field.val();
  499. input_field.val('');
  500. input_field.val(tmpStr);
  501. input_field.css({
  502. width: inputWidth
  503. })
  504. }).trigger('input');
  505. input_field.focusout(function() {
  506. /// update array:
  507. if (input_field.val().trim()) {
  508. if (global_var.type == Types.REAL) {
  509. global_var.value[row][index] = parseFloat(input_field.val().trim());
  510. parent_node.find('.span_value_variable').text(global_var.value[row][index].toFixed(1));
  511. } else {
  512. if (global_var.type == Types.INTEGER) {
  513. global_var.value[row][index] = parseInt(input_field.val().trim());
  514. } else {
  515. global_var.value[row][index] = input_field.val().trim();
  516. }
  517. parent_node.find('.span_value_variable').text(global_var.value[row][index]);
  518. }
  519. registerUserEvent(global_var.name, ActionTypes.CHANGE_GLOBAL_VALUE, global_var.value);
  520. } else {
  521. if (global_var.type == Types.REAL) {
  522. parent_node.find('.span_value_variable').text(global_var.value[row][index].toFixed(1));
  523. } else {
  524. parent_node.find('.span_value_variable').text(global_var.value[row][index]);
  525. }
  526. }
  527. if (global_var.type == Types.TEXT) {
  528. global_var.value[row][index] = input_field.val();
  529. parent_node.find('.span_value_variable').text(global_var.value[row][index]);
  530. }
  531. input_field.off();
  532. input_field.remove();
  533. /// update elements:
  534. opened_name_value_matrix_global_v = false;
  535. opened_input_value_matrix_global_v = false;
  536. });
  537. input_field.on('keydown', function(e) {
  538. var code = e.keyCode || e.which;
  539. if(code == 13) {
  540. if (input_field.val().trim()) {
  541. if (global_var.type == Types.REAL) {
  542. global_var.value[row][index] = parseFloat(input_field.val().trim());
  543. parent_node.find('.span_value_variable').text(global_var.value[row][index].toFixed(1));
  544. } else {
  545. if (global_var.type == Types.INTEGER) {
  546. global_var.value[row][index] = parseInt(input_field.val().trim());
  547. } else {
  548. global_var.value[row][index] = input_field.val().trim();
  549. }
  550. parent_node.find('.span_value_variable').text(global_var.value[row][index]);
  551. }
  552. registerUserEvent(global_var.name, ActionTypes.CHANGE_GLOBAL_VALUE, global_var.value);
  553. } else {
  554. if (global_var.type == Types.REAL) {
  555. parent_node.find('.span_value_variable').text(global_var.value[row][index].toFixed(1));
  556. } else {
  557. parent_node.find('.span_value_variable').text(global_var.value[row][index]);
  558. }
  559. }
  560. if (global_var.type == Types.TEXT) {
  561. global_var.value[row][index] = input_field.val();
  562. registerUserEvent(global_var.name, ActionTypes.CHANGE_GLOBAL_VALUE, global_var.value);
  563. parent_node.find('.span_value_variable').text(global_var.value[row][index]);
  564. }
  565. input_field.off();
  566. input_field.remove();
  567. /// update elements:
  568. opened_name_value_matrix_global_v = false;
  569. opened_input_value_matrix_global_v = false;
  570. }
  571. if(code == 27) {
  572. if (global_var.type == Types.REAL) {
  573. parent_node.find('.span_value_variable').text(global_var.value[row][index].toFixed(1));
  574. } else {
  575. parent_node.find('.span_value_variable').text(global_var.value[row][index]);
  576. }
  577. input_field.off();
  578. input_field.remove();
  579. /// update elements:
  580. opened_name_value_matrix_global_v = false;
  581. opened_input_value_matrix_global_v = false;
  582. }
  583. });
  584. input_field.select();
  585. }
  586. var opened_name_value_global_var = false;
  587. var opened_input_value_global_ar = null;
  588. function enableGlobalValueUpdate (global_var, parent_node) {
  589. if (opened_name_value_global_var) {
  590. opened_input_value_global_ar.focus();
  591. return;
  592. }
  593. parent_node = $(parent_node);
  594. opened_name_value_global_var = true;
  595. parent_node.find('.span_value_variable').text('');
  596. var input_field;
  597. if (global_var.type == Types.REAL) {
  598. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  599. + global_var.value.toFixed(1) + "' />" );
  600. input_field.insertBefore(parent_node.find('.span_value_variable'));
  601. } else {
  602. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  603. + global_var.value + "' />" );
  604. input_field.insertBefore(parent_node.find('.span_value_variable'));
  605. }
  606. input_field.on('input', function() {
  607. var inputWidth = input_field.textWidth()+10;
  608. opened_input_value_global_ar = input_field;
  609. input_field.focus();
  610. var tmpStr = input_field.val();
  611. input_field.val('');
  612. input_field.val(tmpStr);
  613. input_field.css({
  614. width: inputWidth
  615. })
  616. }).trigger('input');
  617. input_field.focusout(function() {
  618. let changed = false;
  619. /// update array:
  620. if (input_field.val().trim()) {
  621. if (global_var.type == Types.REAL) {
  622. global_var.value = parseFloat(input_field.val().trim());
  623. parent_node.find('.span_value_variable').text(global_var.value.toFixed(1));
  624. } else{
  625. if (global_var.type == Types.INTEGER) {
  626. global_var.value = parseInt(input_field.val().trim());
  627. } else {
  628. global_var.value = input_field.val().trim();
  629. }
  630. parent_node.find('.span_value_variable').text(global_var.value);
  631. }
  632. changed = true;
  633. } else {
  634. if (global_var.type == Types.REAL) {
  635. parent_node.find('.span_value_variable').text(global_var.value.toFixed(1));
  636. } else {
  637. parent_node.find('.span_value_variable').text(global_var.value);
  638. }
  639. }
  640. if (global_var.type == Types.TEXT) {
  641. global_var.value = input_field.val();
  642. changed = true;
  643. parent_node.find('.span_value_variable').text(global_var.value);
  644. }
  645. if (changed) {
  646. registerUserEvent(global_var.name, ActionTypes.CHANGE_GLOBAL_VALUE, global_var.value);
  647. }
  648. input_field.off();
  649. input_field.remove();
  650. /// update elements:
  651. opened_name_value_global_var = false;
  652. opened_input_value_global_ar = false;
  653. });
  654. input_field.on('keydown', function(e) {
  655. const code = e.keyCode || e.which;
  656. let changed = true;
  657. if(code == 13) {
  658. if (input_field.val().trim()) {
  659. if (global_var.type == Types.REAL) {
  660. global_var.value = parseFloat(input_field.val().trim());
  661. parent_node.find('.span_value_variable').text(global_var.value.toFixed(1));
  662. } else {
  663. if (global_var.type == Types.INTEGER) {
  664. global_var.value = parseInt(input_field.val().trim());
  665. } else {
  666. global_var.value = input_field.val().trim();
  667. }
  668. parent_node.find('.span_value_variable').text(global_var.value);
  669. }
  670. changed = true;
  671. } else {
  672. if (global_var.type == Types.REAL) {
  673. parent_node.find('.span_value_variable').text(global_var.value.toFixed(1));
  674. } else {
  675. parent_node.find('.span_value_variable').text(global_var.value);
  676. }
  677. }
  678. if (global_var.type == Types.TEXT) {
  679. global_var.value = input_field.val();
  680. changed = true;
  681. parent_node.find('.span_value_variable').text(global_var.value);
  682. }
  683. if (changed) {
  684. registerUserEvent(global_var.name, ActionTypes.CHANGE_GLOBAL_VALUE, global_var.value);
  685. }
  686. input_field.off();
  687. input_field.remove();
  688. /// update elements:
  689. opened_name_value_global_var = false;
  690. opened_input_value_global_ar = false;
  691. }
  692. if(code == 27) {
  693. if (global_var.type == Types.REAL) {
  694. parent_node.find('.span_value_variable').text(global_var.value.toFixed(1));
  695. } else{
  696. parent_node.find('.span_value_variable').text(global_var.value);
  697. }
  698. input_field.off();
  699. input_field.remove();
  700. /// update elements:
  701. opened_name_value_global_var = false;
  702. opened_input_value_global_ar = false;
  703. }
  704. });
  705. input_field.select();
  706. }
  707. var opened_name_global = false;
  708. var opened_input_global = null;
  709. function enableNameUpdate (global_container) {
  710. var global_var = global_container.data('associatedOject');
  711. if (opened_name_global) {
  712. opened_input_global.focus();
  713. return;
  714. }
  715. opened_name_global = true;
  716. global_container.find('.span_name_variable').text('');
  717. var input_name = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+global_var.name+"' />" );
  718. input_name.insertBefore(global_container.find('.span_name_variable'));
  719. input_name.on('input', function() {
  720. var inputWidth = input_name.textWidth()+10;
  721. opened_input_global = input_name;
  722. opened_input_global.focus();
  723. opened_input_global.css({
  724. width: inputWidth
  725. })
  726. }).trigger('input');
  727. input_name.focusout(function() {
  728. /// update array:
  729. if (input_name.val().trim().length > 0) {
  730. updateName(global_var, input_name.val().trim(), global_container);
  731. global_container.find('.span_name_variable').text(global_var.name);
  732. } else {
  733. global_container.find('.span_name_variable').text(global_var.name);
  734. }
  735. input_name.off();
  736. input_name.remove();
  737. /// update elements:
  738. opened_name_global = false;
  739. opened_input_global = false;
  740. });
  741. input_name.on('keydown', function(e) {
  742. var code = e.keyCode || e.which;
  743. if(code == 13) {
  744. if (input_name.val().trim()) {
  745. updateName(global_var, input_name.val().trim(), global_container);
  746. global_container.find('.span_name_variable').text(global_var.name);
  747. } else {
  748. global_container.find('.span_name_variable').text(global_var.name);
  749. }
  750. input_name.off();
  751. input_name.remove();
  752. /// update elements:
  753. opened_name_global = false;
  754. opened_input_global = false;
  755. }
  756. if(code == 27) {
  757. global_container.find('.span_name_variable').text(global_var.name);
  758. input_name.off();
  759. input_name.remove();
  760. /// update elements:
  761. opened_name_global = false;
  762. opened_input_global = false;
  763. }
  764. });
  765. input_name.select();
  766. }
  767. var opened_name_value_vector_global_ = false;
  768. var opened_input_value_vector_global_ = null;
  769. function enableGlobalVectorValueUpdate (global_var, index, parent_node) {
  770. if (opened_name_value_vector_global_) {
  771. opened_input_value_vector_global_.focus();
  772. return;
  773. }
  774. parent_node = $(parent_node);
  775. opened_name_value_vector_global_ = true;
  776. parent_node.find('.span_value_variable').text('');
  777. var input_field;
  778. if (global_var.type == Types.REAL) {
  779. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  780. + global_var.value[index].toFixed(1) + "' />" );
  781. input_field.insertBefore(parent_node.find('.span_value_variable'));
  782. } else {
  783. input_field = $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  784. + global_var.value[index] + "' />" );
  785. input_field.insertBefore(parent_node.find('.span_value_variable'));
  786. }
  787. input_field.on('input', function() {
  788. var inputWidth = input_field.textWidth()+10;
  789. opened_input_value_vector_global_ = input_field;
  790. input_field.focus();
  791. var tmpStr = input_field.val();
  792. input_field.val('');
  793. input_field.val(tmpStr);
  794. input_field.css({
  795. width: inputWidth
  796. })
  797. }).trigger('input');
  798. input_field.focusout(function() {
  799. let changed = false;
  800. /// update array:
  801. if (input_field.val().trim()) {
  802. if (global_var.type == Types.REAL) {
  803. global_var.value[index] = parseFloat(input_field.val().trim());
  804. parent_node.find('.span_value_variable').text(global_var.value[index].toFixed(1));
  805. } else {
  806. if (global_var.type == Types.INTEGER) {
  807. global_var.value[index] = parseInt(input_field.val().trim());
  808. } else {
  809. global_var.value[index] = input_field.val().trim();
  810. }
  811. parent_node.find('.span_value_variable').text(global_var.value[index]);
  812. }
  813. changed = true;
  814. } else {
  815. if (global_var.type == Types.REAL) {
  816. parent_node.find('.span_value_variable').text(global_var.value[index].toFixed(1));
  817. } else {
  818. parent_node.find('.span_value_variable').text(global_var.value[index]);
  819. }
  820. }
  821. if (global_var.type == Types.TEXT) {
  822. global_var.value[index] = input_field.val();
  823. changed = true;
  824. parent_node.find('.span_value_variable').text(global_var.value[index]);
  825. }
  826. if (changed) {
  827. registerUserEvent(global_var.name, ActionTypes.CHANGE_GLOBAL_VALUE, global_var.value);
  828. }
  829. input_field.off();
  830. input_field.remove();
  831. /// update elements:
  832. opened_name_value_vector_global_ = false;
  833. opened_input_value_vector_global_ = false;
  834. });
  835. input_field.on('keydown', function(e) {
  836. const code = e.keyCode || e.which;
  837. let changed = false;
  838. if(code == 13) {
  839. if (input_field.val().trim()) {
  840. if (global_var.type == Types.REAL) {
  841. global_var.value[index] = parseFloat(input_field.val().trim());
  842. parent_node.find('.span_value_variable').text(global_var.value[index].toFixed(1));
  843. } else {
  844. if (global_var.type == Types.INTEGER) {
  845. global_var.value[index] = parseInt(input_field.val().trim());
  846. } else {
  847. global_var.value[index] = input_field.val().trim();
  848. }
  849. parent_node.find('.span_value_variable').text(global_var.value[index]);
  850. }
  851. changed = true;
  852. } else {
  853. if (global_var.type == Types.REAL) {
  854. parent_node.find('.span_value_variable').text(global_var.value[index].toFixed(1));
  855. } else {
  856. parent_node.find('.span_value_variable').text(global_var.value[index]);
  857. }
  858. }
  859. if (global_var.type == Types.TEXT) {
  860. global_var.value[index] = input_field.val();
  861. changed = true;
  862. parent_node.find('.span_value_variable').text(global_var.value[index]);
  863. }
  864. if (changed) {
  865. registerUserEvent(global_var.name, ActionTypes.CHANGE_GLOBAL_VALUE, global_var.value);
  866. }
  867. input_field.off();
  868. input_field.remove();
  869. /// update elements:
  870. opened_name_value_vector_global_ = false;
  871. opened_input_value_vector_global_ = false;
  872. }
  873. if(code == 27) {
  874. if (global_var.type == Types.REAL) {
  875. parent_node.find('.span_value_variable').text(global_var.value[index].toFixed(1));
  876. } else {
  877. parent_node.find('.span_value_variable').text(global_var.value[index]);
  878. }
  879. input_field.off();
  880. input_field.remove();
  881. /// update elements:
  882. opened_name_value_vector_global_ = false;
  883. opened_input_value_vector_global_ = false;
  884. }
  885. });
  886. input_field.select();
  887. }
  888. $.fn.textWidth = function(text, font) {
  889. if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
  890. $.fn.textWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));
  891. return $.fn.textWidth.fakeEl.width();
  892. };