globals_sidebar.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. import $ from 'jquery';
  2. import jQuery from 'jquery';
  3. import { Types } from './types';
  4. import * as Models from './ivprog_elements';
  5. import { LocalizedStrings } from './../services/localizedStringsService';
  6. window.jQuery = jQuery;
  7. import '../semantic/semantic.min.js';
  8. var counter_new_globals = 0;
  9. export function addGlobal (program) {
  10. var new_global = new Models.Variable(Types.INTEGER, LocalizedStrings.getUI('new_global') + '_' + counter_new_globals, 1);
  11. counter_new_globals ++;
  12. program.addGlobal(new_global);
  13. renderGlobal(new_global);
  14. }
  15. function toggleConstant (global_var) {
  16. global_var.is_constant = !global_var.is_constant;
  17. }
  18. function updateName (global_var, new_name) {
  19. global_var.name = new_name;
  20. }
  21. function updateType (global_var, new_type, new_dimensions = 0) {
  22. global_var.type = new_type;
  23. global_var.dimensions = new_dimensions;
  24. if (new_dimensions > 0) {
  25. global_var.rows = new_dimensions;
  26. global_var.columns = 2;
  27. }
  28. updateInitialValues(global_var);
  29. }
  30. function removeGlobal (global_var, global_container) {
  31. var index = window.program_obj.globals.indexOf(global_var);
  32. if (index > -1) {
  33. window.program_obj.globals.splice(index, 1);
  34. }
  35. $(global_container).remove();
  36. }
  37. function updateInitialValues (global_var) {
  38. if (global_var.type == Types.INTEGER) {
  39. if (global_var.dimensions == 0) {
  40. global_var.value = 1;
  41. }
  42. if (global_var.dimensions == 1) {
  43. global_var.value = [1, 1];
  44. }
  45. if (global_var.dimensions == 2) {
  46. global_var.value = [[1, 1], [1, 1]];
  47. }
  48. }
  49. if (global_var.type == Types.REAL) {
  50. if (global_var.dimensions == 0) {
  51. global_var.value = 1.0;
  52. }
  53. if (global_var.dimensions == 1) {
  54. global_var.value = [1.0, 1.0];
  55. }
  56. if (global_var.dimensions == 2) {
  57. global_var.value = [[1.0, 1.0], [1.0, 1.0]];
  58. }
  59. }
  60. if (global_var.type == Types.TEXT) {
  61. if (global_var.dimensions == 0) {
  62. global_var.value = LocalizedStrings.getUI('text_start');
  63. }
  64. if (global_var.dimensions == 1) {
  65. global_var.value = [LocalizedStrings.getUI('text_start'), LocalizedStrings.getUI('text_start')];
  66. }
  67. if (global_var.dimensions == 2) {
  68. global_var.value = [[LocalizedStrings.getUI('text_start'), LocalizedStrings.getUI('text_start')],
  69. [LocalizedStrings.getUI('text_start'), LocalizedStrings.getUI('text_start')]];
  70. }
  71. }
  72. if (global_var.type == Types.BOOLEAN) {
  73. if (global_var.dimensions == 0) {
  74. global_var.value = true;
  75. }
  76. if (global_var.dimensions == 1) {
  77. global_var.value = [true, true];
  78. }
  79. if (global_var.dimensions == 2) {
  80. global_var.value = [[true, true], [true, true]];
  81. }
  82. }
  83. }
  84. function alternateBooleanGlobalValue (global_var, value_container) {
  85. global_var.value = !global_var.value;
  86. $(value_container).find('.span_value_variable').text(global_var.value);
  87. }
  88. function alternateBooleanGlobalVectorValue (global_var, index, value_container) {
  89. global_var.value[index] = !global_var.value[index];
  90. $(value_container).find('.span_value_variable').text(global_var.value[index]);
  91. }
  92. function removeGlobalColumnVector (global_var) {
  93. if (global_var.columns == 0) {
  94. return;
  95. }
  96. global_var.columns --;
  97. global_var.value.splice(global_var.value.length - 1, 1);
  98. }
  99. function addGlobalColumnVector (global_var) {
  100. global_var.columns ++;
  101. if (global_var.type == Types.INTEGER) {
  102. global_var.value.push(1);
  103. }
  104. if (global_var.type == Types.REAL) {
  105. global_var.value.push(1.0);
  106. }
  107. if (global_var.type == Types.TEXT) {
  108. global_var.value.push(LocalizedStrings.getUI('text_start'));
  109. }
  110. if (global_var.type == Types.BOOLEAN) {
  111. global_var.value.push(true);
  112. }
  113. }
  114. function removeColumnGlobalMatrix (global_var) {
  115. if (global_var.columns == 0) {
  116. return;
  117. }
  118. global_var.columns --;
  119. for (var i = 0; i < global_var.rows; i++) {
  120. global_var.value[i].splice(global_var.value[i].length - 1, 1);
  121. }
  122. }
  123. function addColumnGlobalMatrix (global_var) {
  124. global_var.columns ++;
  125. if (global_var.type == Types.INTEGER) {
  126. for (var i = 0; i < global_var.rows; i++) {
  127. global_var.value[i].push(1);
  128. }
  129. }
  130. if (global_var.type == Types.REAL) {
  131. for (var i = 0; i < global_var.rows; i++) {
  132. global_var.value[i].push(1.0);
  133. }
  134. }
  135. if (global_var.type == Types.TEXT) {
  136. for (var i = 0; i < global_var.rows; i++) {
  137. global_var.value[i].push(LocalizedStrings.getUI('text_start'));
  138. }
  139. }
  140. if (global_var.type == Types.BOOLEAN) {
  141. for (var i = 0; i < global_var.rows; i++) {
  142. global_var.value[i].push(true);
  143. }
  144. }
  145. }
  146. function removeLineGlobalMatrix (global_var) {
  147. if (global_var.rows == 0) {
  148. return;
  149. }
  150. global_var.rows --;
  151. global_var.value.splice(global_var.value.length - 1, 1);
  152. }
  153. function addLineGlobalMatrix (global_var) {
  154. global_var.rows ++;
  155. if (global_var.type == Types.INTEGER) {
  156. var n_l = [];
  157. for (var i = 0; i < global_var.columns; i++) {
  158. n_l.push(1);
  159. }
  160. global_var.value.push(n_l);
  161. }
  162. if (global_var.type == Types.REAL) {
  163. var n_l = [];
  164. for (i = 0; i < global_var.columns; i++) {
  165. n_l.push(1.0);
  166. }
  167. global_var.value.push(n_l);
  168. }
  169. if (global_var.type == Types.TEXT) {
  170. var n_l = [];
  171. for (i = 0; i < global_var.columns; i++) {
  172. n_l.push(LocalizedStrings.getUI('text_start'));
  173. }
  174. global_var.value.push(n_l);
  175. }
  176. if (global_var.type == Types.BOOLEAN) {
  177. var n_l = [];
  178. for (i = 0; i < global_var.columns; i++) {
  179. n_l.push(true);
  180. }
  181. global_var.value.push(n_l);
  182. }
  183. }
  184. function alternateBooleanGlobalMatrixValue (global_var, row, index, value_container) {
  185. global_var.value[row][index] = !global_var.value[row][index];
  186. $(value_container).find('.span_value_variable').text(global_var.value[row][index]);
  187. }
  188. function renderValues (global_var, global_container) {
  189. var ret = "";
  190. var j = 0;
  191. if (global_var.dimensions == 0) {
  192. if (global_var.type == Types.REAL) {
  193. ret += '<div class="created_div_valor_var"><span class="span_value_variable simple_var">'+global_var.value.toFixed(1)+'</span> </div> ';
  194. } else {
  195. if (global_var.type == Types.BOOLEAN) {
  196. ret += '<div class="created_div_valor_var"><span class="span_value_variable boolean_simple_type">'+global_var.value+'</span> </div> ';
  197. } else {
  198. ret += '<div class="created_div_valor_var"><span class="span_value_variable simple_var">'+global_var.value+'</span> </div> ';
  199. }
  200. }
  201. } else {
  202. ret += '<table class="tabela_var">';
  203. if (global_var.dimensions == 1) {
  204. ret += '<tr>';
  205. if (global_var.type == Types.REAL) {
  206. for (var k = 0; k < global_var.columns; k++) {
  207. ret += '<td><span class="span_value_variable vector_var" data-index="'+k+'">'+global_var.value[k].toFixed(1)+'</span></td>';
  208. }
  209. } else {
  210. for (var k = 0; k < global_var.columns; k++) {
  211. if (global_var.type == Types.BOOLEAN) {
  212. ret += '<td><span class="span_value_variable boolean_vector_var" data-index="'+k+'">'+global_var.value[k]+'</span></td>';
  213. } else {
  214. ret += '<td><span class="span_value_variable vector_var" data-index="'+k+'">'+global_var.value[k]+'</span>'+'</td>';
  215. }
  216. }
  217. }
  218. ret += '</tr>';
  219. ret += '</table>';
  220. ret += '<div class="buttons_manage_columns"><i class="ui icon minus square outline remove_global_vector_column"></i>'
  221. + ' <i class="ui icon plus square outline add_global_vector_column"></i></div>';
  222. }
  223. if (global_var.dimensions == 2) {
  224. if (global_var.type == Types.REAL) {
  225. for (var l = 0; l < global_var.rows; l++) {
  226. ret += '<tr>';
  227. for (var k = 0; k < global_var.columns; k++) {
  228. ret += '<td><span class="span_value_variable matrix_var" data-index="'+k+'" data-row="'+l+'">'+global_var.value[l][k].toFixed(1)+'</span>'+'</td>';
  229. }
  230. ret += '</tr>';
  231. }
  232. } else {
  233. for (var l = 0; l < global_var.rows; l++) {
  234. ret += '<tr>';
  235. for (var k = 0; k < global_var.columns; k++) {
  236. if (global_var.type == Types.BOOLEAN) {
  237. ret += '<td><span class="span_value_variable boolean_matrix_var" data-index="'+k+'" data-row="'+l+'">'+global_var.value[l][k]+'</span></td>';
  238. } else {
  239. ret += '<td><span class="span_value_variable matrix_var" data-index="'+k+'" data-row="'+l+'">'+global_var.value[l][k]+'</span></td>';
  240. }
  241. }
  242. ret += '</tr>';
  243. }
  244. }
  245. if (global_var.rows == 0) {
  246. ret += '<tr><td></td></tr>';
  247. }
  248. ret += '<tr><td colspan="'+global_var.columns+'" class="tr_manage_lines"><i class="ui icon minus square outline remove_global_matrix_line"></i>'
  249. + ' <i class="ui icon plus square outline add_global_matrix_line"></i></td></tr>';
  250. ret += '</table>';
  251. ret += '<div class="buttons_manage_columns"><i class="ui icon minus square outline remove_global_matrix_column"></i>'
  252. + ' <i class="ui icon plus square outline add_global_matrix_column"></i></div>';
  253. }
  254. }
  255. $( global_container ).find( ".div_valor_var" ).html('');
  256. ret = $(ret);
  257. $(ret).find('.span_value_variable').data('associatedOject', global_var);
  258. $( ret ).find( ".boolean_simple_type" ).on('click', function(e){
  259. alternateBooleanGlobalValue(global_var, this.parentNode);
  260. });
  261. $( ret ).find( ".simple_var" ).on('click', function(e){
  262. enableGlobalValueUpdate(global_var, this.parentNode);
  263. });
  264. $( ret ).find( ".boolean_vector_var" ).on('click', function(e){
  265. alternateBooleanGlobalVectorValue(global_var, $(this).data('index'), this.parentNode);
  266. });
  267. $( ret ).find( ".vector_var" ).on('click', function(e){
  268. enableGlobalVectorValueUpdate(global_var, $(this).data('index'), this.parentNode);
  269. });
  270. $( ret ).find( ".remove_global_vector_column" ).on('click', function(e){
  271. removeGlobalColumnVector(global_var);
  272. $( global_container ).find( ".div_valor_var" ).html('');
  273. renderValues(global_var, global_container);
  274. });
  275. $( ret ).find( ".add_global_vector_column" ).on('click', function(e){
  276. addGlobalColumnVector(global_var);
  277. $( global_container ).find( ".div_valor_var" ).html('');
  278. renderValues(global_var, global_container);
  279. });
  280. $( ret ).find( ".remove_global_matrix_column" ).on('click', function(e){
  281. removeColumnGlobalMatrix(global_var);
  282. $( global_container ).find( ".div_valor_var" ).html('');
  283. renderValues(global_var, global_container);
  284. });
  285. $( ret ).find( ".add_global_matrix_column" ).on('click', function(e){
  286. addColumnGlobalMatrix(global_var);
  287. $( global_container ).find( ".div_valor_var" ).html('');
  288. renderValues(global_var, global_container);
  289. });
  290. $( ret ).find( ".remove_global_matrix_line" ).on('click', function(e){
  291. removeLineGlobalMatrix(global_var);
  292. $( global_container ).find( ".div_valor_var" ).html('');
  293. renderValues(global_var, global_container);
  294. });
  295. $( ret ).find( ".add_global_matrix_line" ).on('click', function(e){
  296. addLineGlobalMatrix(global_var);
  297. $( global_container ).find( ".div_valor_var" ).html('');
  298. renderValues(global_var, global_container);
  299. });
  300. $( ret ).find( ".boolean_matrix_var" ).on('click', function(e){
  301. alternateBooleanGlobalMatrixValue(global_var, $(this).data('row'), $(this).data('index'), this.parentNode);
  302. });
  303. $( ret ).find( ".matrix_var" ).on('click', function(e){
  304. enableGlobalMatrixValueUpdate(global_var, $(this).data('row'), $(this).data('index'), this.parentNode);
  305. });
  306. $( global_container ).find( ".div_valor_var" ).append(ret);
  307. updateColumnsAndRowsText(global_container, global_var);
  308. }
  309. function addHandlers (global_container) {
  310. var global_var = global_container.data('associatedOject');
  311. // Manage constant option:
  312. global_container.find( ".alternate_constant" ).on('click', function(e){
  313. toggleConstant(global_var);
  314. $( this ).removeClass( "on off" );
  315. if (global_var.is_constant) {
  316. $( this ).addClass( "on" );
  317. } else {
  318. $( this ).addClass( "off" );
  319. }
  320. });
  321. // Manage global name:
  322. global_container.find( ".enable_edit_name_parameter" ).on('click', function(e){
  323. enableNameUpdate(global_container);
  324. });
  325. // Menu to change type:
  326. global_container.find('.ui.dropdown.global_type').dropdown({
  327. onChange: function(value, text, $selectedItem) {
  328. if ($($selectedItem).data('dimensions')) {
  329. updateType(global_var, Types[$($selectedItem).data('type')], $($selectedItem).data('dimensions'));
  330. } else {
  331. updateType(global_var, Types[$($selectedItem).data('type')]);
  332. }
  333. renderValues(global_var, global_container);
  334. }
  335. });
  336. // Remove global:
  337. global_container.find( ".remove_global" ).on('click', function(e){
  338. removeGlobal(global_var, global_container);
  339. });
  340. global_container
  341. .on('dragstart', function(e) {
  342. e.originalEvent.dataTransfer.setData("text",JSON.stringify({type:"var",content:global_var}));
  343. })
  344. }
  345. function updateColumnsAndRowsText (global_container, global_var) {
  346. var prev = global_container.find('.text').text().split('[');
  347. if (prev.length == 2) {
  348. var ff = prev[0] + '[ ' + global_var.columns + ' ] ';
  349. global_container.find('.text').empty();
  350. global_container.find('.text').text(ff);
  351. }
  352. if (prev.length == 3) {
  353. var ff = prev[0] + '[ ' + global_var.columns + ' ] [ ' + global_var.rows + ' ] ';
  354. global_container.find('.text').empty();
  355. global_container.find('.text').text(ff);
  356. }
  357. }
  358. export function renderGlobal (global_var) {
  359. var element = '<div class="ui label global_container"><div class="global_const">const: ';
  360. element += '<i class="ui icon toggle '+(global_var.is_constant?"on":"off")+' alternate_constant"></i></div>';
  361. element += '<div class="ui dropdown global_type">';
  362. if (global_var.dimensions > 0) {
  363. element += '<div class="text">'+ i18n('ui:vector') + ':' + LocalizedStrings.getUI(global_var.type);
  364. for (var i = 0; i < global_var.dimensions; i ++) {
  365. element += ' [ <span class="dimensions_'+i+'"></span> ] ';
  366. }
  367. element += '</div>';
  368. } else {
  369. element += '<div class="text">' + LocalizedStrings.getUI(global_var.type.toLowerCase()) + '</div>';
  370. }
  371. element += '<div class="menu">';
  372. for (var tm in Types) {
  373. if (tm == Types.VOID.toUpperCase()) {
  374. continue;
  375. }
  376. element += '<div class="item ' + (global_var.type == tm.toLowerCase() ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(tm.toLowerCase())+'</div>';
  377. }
  378. for (var tm in Types) {
  379. if (tm == Types.VOID.toUpperCase()) {
  380. continue;
  381. }
  382. element += '<div class="item">'
  383. + '<i class="dropdown icon"></i>'
  384. + LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())
  385. + '<div class="menu">'
  386. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] " data-type="'+tm+'" data-dimensions="1">[ ]</div>'
  387. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] [ ] " data-type="'+tm+'" data-dimensions="2">[ ] [ ] </div>'
  388. + '</div>'
  389. + '</div>';
  390. }
  391. element += '</div></div> <div class="editing_name_var"> <span class="span_name_variable enable_edit_name_parameter">'+global_var.name+'</span> </div> <span class="character_equals"> = </span> ';
  392. element += '<div class="ui div_valor_var">'+global_var.value+'</div>';
  393. element += ' <i class="red icon times remove_global"></i></div>';
  394. var complete_element = $(element);
  395. complete_element.data('associatedOject', global_var);
  396. $('.list_globals').append(complete_element);
  397. addHandlers(complete_element);
  398. renderValues(global_var, complete_element);
  399. if (global_var.dimensions == 1) {
  400. complete_element.find('.dimensions_0').text(global_var.columns);
  401. }
  402. if (global_var.dimensions == 2) {
  403. complete_element.find('.dimensions_0').text(global_var.columns);
  404. complete_element.find('.dimensions_1').text(global_var.rows);
  405. }
  406. }
  407. var opened_name_value_matrix_global_v = false;
  408. var opened_input_value_matrix_global_v = null;
  409. function enableGlobalMatrixValueUpdate (global_var, row, index, parent_node) {
  410. if (opened_name_value_matrix_global_v) {
  411. $(opened_input_value_matrix_global_v).focus();
  412. return;
  413. }
  414. opened_name_value_matrix_global_v = true;
  415. $(parent_node).find('.span_value_variable').text('');
  416. if (global_var.type == Types.REAL) {
  417. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  418. + global_var.value[row][index].toFixed(1) + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  419. } else {
  420. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  421. + global_var.value[row][index] + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  422. }
  423. $('.width-dynamic').on('input', function() {
  424. var inputWidth = $(this).textWidth()+10;
  425. opened_input_value_matrix_global_v = this;
  426. $(this).focus();
  427. var tmpStr = $(this).val();
  428. $(this).val('');
  429. $(this).val(tmpStr);
  430. $(this).css({
  431. width: inputWidth
  432. })
  433. }).trigger('input');
  434. $('.width-dynamic').focusout(function() {
  435. /// update array:
  436. if ($(this).val().trim()) {
  437. if (global_var.type == Types.REAL) {
  438. global_var.value[row][index] = parseFloat($(this).val().trim());
  439. $(parent_node).find('.span_value_variable').text(global_var.value[row][index].toFixed(1));
  440. } else {
  441. if (global_var.type == Types.INTEGER) {
  442. global_var.value[row][index] = parseInt($(this).val().trim());
  443. } else {
  444. global_var.value[row][index] = $(this).val().trim();
  445. }
  446. $(parent_node).find('.span_value_variable').text(global_var.value[row][index]);
  447. }
  448. }
  449. $(this).remove();
  450. /// update elements:
  451. opened_name_value_matrix_global_v = false;
  452. opened_input_value_matrix_global_v = false;
  453. });
  454. $('.width-dynamic').on('keydown', function(e) {
  455. var code = e.keyCode || e.which;
  456. if(code == 13) {
  457. if ($(this).val().trim()) {
  458. if (global_var.type == Types.REAL) {
  459. global_var.value[row][index] = parseFloat($(this).val().trim());
  460. $(parent_node).find('.span_value_variable').text(global_var.value[row][index].toFixed(1));
  461. } else {
  462. if (global_var.type == Types.INTEGER) {
  463. global_var.value[row][index] = parseInt($(this).val().trim());
  464. } else {
  465. global_var.value[row][index] = $(this).val().trim();
  466. }
  467. $(parent_node).find('.span_value_variable').text(global_var.value[row][index]);
  468. }
  469. }
  470. $(this).remove();
  471. /// update elements:
  472. opened_name_value_matrix_global_v = false;
  473. opened_input_value_matrix_global_v = false;
  474. }
  475. if(code == 27) {
  476. if (global_var.type == Types.REAL) {
  477. $(parent_node).find('.span_value_variable').text(global_var.value[row][index].toFixed(1));
  478. } else {
  479. $(parent_node).find('.span_value_variable').text(global_var.value[row][index]);
  480. }
  481. $(this).remove();
  482. /// update elements:
  483. opened_name_value_matrix_global_v = false;
  484. opened_input_value_matrix_global_v = false;
  485. }
  486. });
  487. }
  488. var opened_name_value_global_var = false;
  489. var opened_input_value_global_ar = null;
  490. function enableGlobalValueUpdate (global_var, parent_node) {
  491. if (opened_name_value_global_var) {
  492. $(opened_input_value_global_ar).focus();
  493. return;
  494. }
  495. opened_name_value_global_var = true;
  496. $(parent_node).find('.span_value_variable').text('');
  497. if (global_var.type == Types.REAL) {
  498. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  499. + global_var.value.toFixed(1) + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  500. } else {
  501. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  502. + global_var.value + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  503. }
  504. $('.width-dynamic').on('input', function() {
  505. var inputWidth = $(this).textWidth()+10;
  506. opened_input_value_global_ar = this;
  507. $(this).focus();
  508. var tmpStr = $(this).val();
  509. $(this).val('');
  510. $(this).val(tmpStr);
  511. $(this).css({
  512. width: inputWidth
  513. })
  514. }).trigger('input');
  515. $('.width-dynamic').focusout(function() {
  516. /// update array:
  517. if ($(this).val().trim()) {
  518. if (global_var.type == Types.REAL) {
  519. global_var.value = parseFloat($(this).val().trim());
  520. $(parent_node).find('.span_value_variable').text(global_var.value.toFixed(1));
  521. } else{
  522. if (global_var.type == Types.INTEGER) {
  523. global_var.value = parseInt($(this).val().trim());
  524. } else {
  525. global_var.value = $(this).val().trim();
  526. }
  527. $(parent_node).find('.span_value_variable').text(global_var.value);
  528. }
  529. }
  530. $(this).remove();
  531. /// update elements:
  532. opened_name_value_global_var = false;
  533. opened_input_value_global_ar = false;
  534. });
  535. $('.width-dynamic').on('keydown', function(e) {
  536. var code = e.keyCode || e.which;
  537. if(code == 13) {
  538. if ($(this).val().trim()) {
  539. if (global_var.type == Types.REAL) {
  540. global_var.value = parseFloat($(this).val().trim());
  541. $(parent_node).find('.span_value_variable').text(global_var.value.toFixed(1));
  542. } else{
  543. if (global_var.type == Types.INTEGER) {
  544. global_var.value = parseInt($(this).val().trim());
  545. } else {
  546. global_var.value = $(this).val().trim();
  547. }
  548. $(parent_node).find('.span_value_variable').text(global_var.value);
  549. }
  550. }
  551. $(this).remove();
  552. /// update elements:
  553. opened_name_value_global_var = false;
  554. opened_input_value_global_ar = false;
  555. }
  556. if(code == 27) {
  557. if (global_var.type == Types.REAL) {
  558. $(parent_node).find('.span_value_variable').text(global_var.value.toFixed(1));
  559. } else{
  560. $(parent_node).find('.span_value_variable').text(global_var.value);
  561. }
  562. $(this).remove();
  563. /// update elements:
  564. opened_name_value_global_var = false;
  565. opened_input_value_global_ar = false;
  566. }
  567. });
  568. }
  569. var opened_name_global = false;
  570. var opened_input_global = null;
  571. function enableNameUpdate (global_container) {
  572. var global_var = global_container.data('associatedOject');
  573. if (opened_name_global) {
  574. opened_input_global.focus();
  575. return;
  576. }
  577. opened_name_global = true;
  578. global_container.find('.span_name_variable').text('');
  579. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"+global_var.name+"' />" ).insertBefore(global_container.find('.span_name_variable'));
  580. $('.width-dynamic').on('input', function() {
  581. var inputWidth = $(this).textWidth()+10;
  582. opened_input_global = $(this);
  583. opened_input_global.focus();
  584. opened_input_global.css({
  585. width: inputWidth
  586. })
  587. }).trigger('input');
  588. $('.width-dynamic').focusout(function() {
  589. /// update array:
  590. if ($(this).val().trim().length > 0) {
  591. updateName(global_var, $(this).val().trim());
  592. global_container.find('.span_name_variable').text(global_var.name);
  593. } else {
  594. global_container.find('.span_name_variable').text(global_var.name);
  595. }
  596. $(this).remove();
  597. /// update elements:
  598. opened_name_global = false;
  599. opened_input_global = false;
  600. });
  601. $('.width-dynamic').on('keydown', function(e) {
  602. var code = e.keyCode || e.which;
  603. if(code == 13) {
  604. if ($(this).val().trim()) {
  605. updateName(global_var, $(this).val().trim());
  606. global_container.find('.span_name_variable').text(global_var.name);
  607. } else {
  608. global_container.find('.span_name_variable').text(global_var.name);
  609. }
  610. $(this).remove();
  611. /// update elements:
  612. opened_name_global = false;
  613. opened_input_global = false;
  614. }
  615. if(code == 27) {
  616. global_container.find('.span_name_variable').text(global_var.name);
  617. $(this).remove();
  618. /// update elements:
  619. opened_name_global = false;
  620. opened_input_global = false;
  621. }
  622. });
  623. $('.width-dynamic').select();
  624. }
  625. var opened_name_value_vector_global_ = false;
  626. var opened_input_value_vector_global_ = null;
  627. function enableGlobalVectorValueUpdate (global_var, index, parent_node) {
  628. if (opened_name_value_vector_global_) {
  629. $(opened_input_value_vector_global_).focus();
  630. return;
  631. }
  632. opened_name_value_vector_global_ = true;
  633. $(parent_node).find('.span_value_variable').text('');
  634. if (global_var.type == Types.REAL) {
  635. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  636. + global_var.value[index].toFixed(1) + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  637. } else {
  638. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  639. + global_var.value[index] + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  640. }
  641. $('.width-dynamic').on('input', function() {
  642. var inputWidth = $(this).textWidth()+10;
  643. opened_input_value_vector_global_ = this;
  644. $(this).focus();
  645. var tmpStr = $(this).val();
  646. $(this).val('');
  647. $(this).val(tmpStr);
  648. $(this).css({
  649. width: inputWidth
  650. })
  651. }).trigger('input');
  652. $('.width-dynamic').focusout(function() {
  653. /// update array:
  654. if ($(this).val().trim()) {
  655. if (global_var.type == Types.REAL) {
  656. global_var.value[index] = parseFloat($(this).val().trim());
  657. $(parent_node).find('.span_value_variable').text(global_var.value[index].toFixed(1));
  658. } else {
  659. if (global_var.type == Types.INTEGER) {
  660. global_var.value[index] = parseInt($(this).val().trim());
  661. } else {
  662. global_var.value[index] = $(this).val().trim();
  663. }
  664. $(parent_node).find('.span_value_variable').text(global_var.value[index]);
  665. }
  666. }
  667. $(this).remove();
  668. /// update elements:
  669. opened_name_value_vector_global_ = false;
  670. opened_input_value_vector_global_ = false;
  671. });
  672. $('.width-dynamic').on('keydown', function(e) {
  673. var code = e.keyCode || e.which;
  674. if(code == 13) {
  675. if ($(this).val().trim()) {
  676. if (global_var.type == Types.REAL) {
  677. global_var.value[index] = parseFloat($(this).val().trim());
  678. $(parent_node).find('.span_value_variable').text(global_var.value[index].toFixed(1));
  679. } else {
  680. if (global_var.type == Types.INTEGER) {
  681. global_var.value[index] = parseInt($(this).val().trim());
  682. } else {
  683. global_var.value[index] = $(this).val().trim();
  684. }
  685. $(parent_node).find('.span_value_variable').text(global_var.value[index]);
  686. }
  687. }
  688. $(this).remove();
  689. /// update elements:
  690. opened_name_value_vector_global_ = false;
  691. opened_input_value_vector_global_ = false;
  692. }
  693. if(code == 27) {
  694. if (global_var.type == Types.REAL) {
  695. $(parent_node).find('.span_value_variable').text(global_var.value[index].toFixed(1));
  696. } else {
  697. $(parent_node).find('.span_value_variable').text(global_var.value[index]);
  698. }
  699. $(this).remove();
  700. /// update elements:
  701. opened_name_value_vector_global_ = false;
  702. opened_input_value_vector_global_ = false;
  703. }
  704. });
  705. }
  706. $.fn.textWidth = function(text, font) {
  707. if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
  708. $.fn.textWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));
  709. return $.fn.textWidth.fakeEl.width();
  710. };
  711. /*************************************************
  712. DOUGLAS
  713. **************************************************/
  714. renderGlobal = function (global_var) {
  715. var element = '<div class="ui container segment global_container"><div class="global_const">const: ';
  716. element += '<i class="ui icon toggle '+(global_var.is_constant?"on":"off")+' alternate_constant"></i></div><span class="span_name_variable enable_edit_name_parameter">'+global_var.name+'</span> <i class="icon small pencil alternate enable_edit_name_parameter"></i>';
  717. element += '<div class="ui dropdown global_type">';
  718. if (global_var.dimensions > 0) {
  719. element += '<div class="text">'+ i18n('ui:vector') + ':' + LocalizedStrings.getUI(global_var.type);
  720. for (var i = 0; i < global_var.dimensions; i ++) {
  721. element += ' [ ] ';
  722. }
  723. element += '</div>';
  724. } else {
  725. element += '<div class="text">' + LocalizedStrings.getUI(global_var.type.toLowerCase()) + '</div>';
  726. }
  727. element += '<i class="dropdown icon"></i><div class="menu">';
  728. for (var tm in Types) {
  729. if (tm == Types.VOID.toUpperCase()) {
  730. continue;
  731. }
  732. element += '<div class="item ' + (global_var.type == tm.toLowerCase() ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(tm.toLowerCase())+'</div>';
  733. }
  734. for (var tm in Types) {
  735. if (tm == Types.VOID.toUpperCase()) {
  736. continue;
  737. }
  738. element += '<div class="item">'
  739. + '<i class="dropdown icon"></i>'
  740. + LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())
  741. + '<div class="menu">'
  742. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] " data-type="'+tm+'" data-dimensions="1">[ ]</div>'
  743. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] [ ] " data-type="'+tm+'" data-dimensions="2">[ ] [ ] </div>'
  744. + '</div>'
  745. + '</div>';
  746. }
  747. element += '</div></div> = ';
  748. element += '<div class="ui div_valor_var">'+global_var.value+'</div>';
  749. element += ' <i class="red icon times remove_global"></i></div>';
  750. var complete_element = $(element);
  751. $(complete_element).data('associatedOject', global_var);
  752. $('.list_globals').append(complete_element);
  753. addHandlers(complete_element);
  754. renderValues(global_var, complete_element);
  755. }