globals_sidebar.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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. program_obj.dataTransfer = {type:"var",content:global_var};
  343. })
  344. global_container
  345. .on('click', function (evt) {
  346. $(this).trigger('dragstart');
  347. if(window.ghostNode) {
  348. $(window.ghostNode).remove();
  349. $(document).off('mousemove');
  350. }
  351. window.ghostNode = $(this).clone();
  352. ghostNode.outerWidth($(this).outerWidth());
  353. ghostNode.draggable().appendTo('body');
  354. ghostNode.css('position', 'absolute');
  355. ghostNode.css('left', evt.pageX);
  356. ghostNode.css('top', evt.pageY);
  357. evt.type = 'drag';
  358. evt.target = ghostNode[0];
  359. ghostNode.trigger(evt);
  360. $(document).on('mousemove', function (evt) {
  361. ghostNode.css('left', evt.pageX);
  362. ghostNode.css('top', evt.pageY);
  363. });
  364. });
  365. }
  366. function updateColumnsAndRowsText (global_container, global_var) {
  367. var prev = global_container.find('.text').text().split('[');
  368. if (prev.length == 2) {
  369. var ff = prev[0] + '[ ' + global_var.columns + ' ] ';
  370. global_container.find('.text').empty();
  371. global_container.find('.text').text(ff);
  372. }
  373. if (prev.length == 3) {
  374. var ff = prev[0] + '[ ' + global_var.columns + ' ] [ ' + global_var.rows + ' ] ';
  375. global_container.find('.text').empty();
  376. global_container.find('.text').text(ff);
  377. }
  378. }
  379. export function renderGlobal (global_var) {
  380. var element = '<div class="ui label global_container"><div class="global_const">const: ';
  381. element += '<i class="ui icon toggle '+(global_var.is_constant?"on":"off")+' alternate_constant"></i></div>';
  382. element += '<div class="ui dropdown global_type">';
  383. if (global_var.dimensions > 0) {
  384. element += '<div class="text">'+ i18n('ui:vector') + ':' + LocalizedStrings.getUI(global_var.type);
  385. for (var i = 0; i < global_var.dimensions; i ++) {
  386. element += ' [ <span class="dimensions_'+i+'"></span> ] ';
  387. }
  388. element += '</div>';
  389. } else {
  390. element += '<div class="text">' + LocalizedStrings.getUI(global_var.type.toLowerCase()) + '</div>';
  391. }
  392. element += '<div class="menu">';
  393. for (var tm in Types) {
  394. if (tm == Types.VOID.toUpperCase()) {
  395. continue;
  396. }
  397. element += '<div class="item ' + (global_var.type == tm.toLowerCase() ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(tm.toLowerCase())+'</div>';
  398. }
  399. for (var tm in Types) {
  400. if (tm == Types.VOID.toUpperCase()) {
  401. continue;
  402. }
  403. element += '<div class="item">'
  404. + '<i class="dropdown icon"></i>'
  405. + LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())
  406. + '<div class="menu">'
  407. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] " data-type="'+tm+'" data-dimensions="1">[ ]</div>'
  408. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] [ ] " data-type="'+tm+'" data-dimensions="2">[ ] [ ] </div>'
  409. + '</div>'
  410. + '</div>';
  411. }
  412. 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> ';
  413. element += '<div class="ui div_valor_var">'+global_var.value+'</div>';
  414. element += ' <i class="red icon times remove_global"></i></div>';
  415. var complete_element = $(element);
  416. complete_element.data('associatedOject', global_var);
  417. $('.list_globals').append(complete_element);
  418. addHandlers(complete_element);
  419. renderValues(global_var, complete_element);
  420. if (global_var.dimensions == 1) {
  421. complete_element.find('.dimensions_0').text(global_var.columns);
  422. }
  423. if (global_var.dimensions == 2) {
  424. complete_element.find('.dimensions_0').text(global_var.columns);
  425. complete_element.find('.dimensions_1').text(global_var.rows);
  426. }
  427. }
  428. var opened_name_value_matrix_global_v = false;
  429. var opened_input_value_matrix_global_v = null;
  430. function enableGlobalMatrixValueUpdate (global_var, row, index, parent_node) {
  431. if (opened_name_value_matrix_global_v) {
  432. $(opened_input_value_matrix_global_v).focus();
  433. return;
  434. }
  435. opened_name_value_matrix_global_v = true;
  436. $(parent_node).find('.span_value_variable').text('');
  437. if (global_var.type == Types.REAL) {
  438. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  439. + global_var.value[row][index].toFixed(1) + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  440. } else {
  441. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  442. + global_var.value[row][index] + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  443. }
  444. $('.width-dynamic').on('input', function() {
  445. var inputWidth = $(this).textWidth()+10;
  446. opened_input_value_matrix_global_v = this;
  447. $(this).focus();
  448. var tmpStr = $(this).val();
  449. $(this).val('');
  450. $(this).val(tmpStr);
  451. $(this).css({
  452. width: inputWidth
  453. })
  454. }).trigger('input');
  455. $('.width-dynamic').focusout(function() {
  456. /// update array:
  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. $('.width-dynamic').on('keydown', function(e) {
  476. var code = e.keyCode || e.which;
  477. if(code == 13) {
  478. if ($(this).val().trim()) {
  479. if (global_var.type == Types.REAL) {
  480. global_var.value[row][index] = parseFloat($(this).val().trim());
  481. $(parent_node).find('.span_value_variable').text(global_var.value[row][index].toFixed(1));
  482. } else {
  483. if (global_var.type == Types.INTEGER) {
  484. global_var.value[row][index] = parseInt($(this).val().trim());
  485. } else {
  486. global_var.value[row][index] = $(this).val().trim();
  487. }
  488. $(parent_node).find('.span_value_variable').text(global_var.value[row][index]);
  489. }
  490. }
  491. $(this).remove();
  492. /// update elements:
  493. opened_name_value_matrix_global_v = false;
  494. opened_input_value_matrix_global_v = false;
  495. }
  496. if(code == 27) {
  497. if (global_var.type == Types.REAL) {
  498. $(parent_node).find('.span_value_variable').text(global_var.value[row][index].toFixed(1));
  499. } else {
  500. $(parent_node).find('.span_value_variable').text(global_var.value[row][index]);
  501. }
  502. $(this).remove();
  503. /// update elements:
  504. opened_name_value_matrix_global_v = false;
  505. opened_input_value_matrix_global_v = false;
  506. }
  507. });
  508. }
  509. var opened_name_value_global_var = false;
  510. var opened_input_value_global_ar = null;
  511. function enableGlobalValueUpdate (global_var, parent_node) {
  512. if (opened_name_value_global_var) {
  513. $(opened_input_value_global_ar).focus();
  514. return;
  515. }
  516. opened_name_value_global_var = true;
  517. $(parent_node).find('.span_value_variable').text('');
  518. if (global_var.type == Types.REAL) {
  519. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  520. + global_var.value.toFixed(1) + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  521. } else {
  522. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  523. + global_var.value + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  524. }
  525. $('.width-dynamic').on('input', function() {
  526. var inputWidth = $(this).textWidth()+10;
  527. opened_input_value_global_ar = this;
  528. $(this).focus();
  529. var tmpStr = $(this).val();
  530. $(this).val('');
  531. $(this).val(tmpStr);
  532. $(this).css({
  533. width: inputWidth
  534. })
  535. }).trigger('input');
  536. $('.width-dynamic').focusout(function() {
  537. /// update array:
  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. $('.width-dynamic').on('keydown', function(e) {
  557. var code = e.keyCode || e.which;
  558. if(code == 13) {
  559. if ($(this).val().trim()) {
  560. if (global_var.type == Types.REAL) {
  561. global_var.value = parseFloat($(this).val().trim());
  562. $(parent_node).find('.span_value_variable').text(global_var.value.toFixed(1));
  563. } else{
  564. if (global_var.type == Types.INTEGER) {
  565. global_var.value = parseInt($(this).val().trim());
  566. } else {
  567. global_var.value = $(this).val().trim();
  568. }
  569. $(parent_node).find('.span_value_variable').text(global_var.value);
  570. }
  571. }
  572. $(this).remove();
  573. /// update elements:
  574. opened_name_value_global_var = false;
  575. opened_input_value_global_ar = false;
  576. }
  577. if(code == 27) {
  578. if (global_var.type == Types.REAL) {
  579. $(parent_node).find('.span_value_variable').text(global_var.value.toFixed(1));
  580. } else{
  581. $(parent_node).find('.span_value_variable').text(global_var.value);
  582. }
  583. $(this).remove();
  584. /// update elements:
  585. opened_name_value_global_var = false;
  586. opened_input_value_global_ar = false;
  587. }
  588. });
  589. }
  590. var opened_name_global = false;
  591. var opened_input_global = null;
  592. function enableNameUpdate (global_container) {
  593. var global_var = global_container.data('associatedOject');
  594. if (opened_name_global) {
  595. opened_input_global.focus();
  596. return;
  597. }
  598. opened_name_global = true;
  599. global_container.find('.span_name_variable').text('');
  600. $( "<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'));
  601. $('.width-dynamic').on('input', function() {
  602. var inputWidth = $(this).textWidth()+10;
  603. opened_input_global = $(this);
  604. opened_input_global.focus();
  605. opened_input_global.css({
  606. width: inputWidth
  607. })
  608. }).trigger('input');
  609. $('.width-dynamic').focusout(function() {
  610. /// update array:
  611. if ($(this).val().trim().length > 0) {
  612. updateName(global_var, $(this).val().trim());
  613. global_container.find('.span_name_variable').text(global_var.name);
  614. } else {
  615. global_container.find('.span_name_variable').text(global_var.name);
  616. }
  617. $(this).remove();
  618. /// update elements:
  619. opened_name_global = false;
  620. opened_input_global = false;
  621. });
  622. $('.width-dynamic').on('keydown', function(e) {
  623. var code = e.keyCode || e.which;
  624. if(code == 13) {
  625. if ($(this).val().trim()) {
  626. updateName(global_var, $(this).val().trim());
  627. global_container.find('.span_name_variable').text(global_var.name);
  628. } else {
  629. global_container.find('.span_name_variable').text(global_var.name);
  630. }
  631. $(this).remove();
  632. /// update elements:
  633. opened_name_global = false;
  634. opened_input_global = false;
  635. }
  636. if(code == 27) {
  637. global_container.find('.span_name_variable').text(global_var.name);
  638. $(this).remove();
  639. /// update elements:
  640. opened_name_global = false;
  641. opened_input_global = false;
  642. }
  643. });
  644. $('.width-dynamic').select();
  645. }
  646. var opened_name_value_vector_global_ = false;
  647. var opened_input_value_vector_global_ = null;
  648. function enableGlobalVectorValueUpdate (global_var, index, parent_node) {
  649. if (opened_name_value_vector_global_) {
  650. $(opened_input_value_vector_global_).focus();
  651. return;
  652. }
  653. opened_name_value_vector_global_ = true;
  654. $(parent_node).find('.span_value_variable').text('');
  655. if (global_var.type == Types.REAL) {
  656. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  657. + global_var.value[index].toFixed(1) + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  658. } else {
  659. $( "<input type='text' class='width-dynamic input_name_function' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' value='"
  660. + global_var.value[index] + "' />" ).insertBefore($(parent_node).find('.span_value_variable'));
  661. }
  662. $('.width-dynamic').on('input', function() {
  663. var inputWidth = $(this).textWidth()+10;
  664. opened_input_value_vector_global_ = this;
  665. $(this).focus();
  666. var tmpStr = $(this).val();
  667. $(this).val('');
  668. $(this).val(tmpStr);
  669. $(this).css({
  670. width: inputWidth
  671. })
  672. }).trigger('input');
  673. $('.width-dynamic').focusout(function() {
  674. /// update array:
  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. $('.width-dynamic').on('keydown', function(e) {
  694. var code = e.keyCode || e.which;
  695. if(code == 13) {
  696. if ($(this).val().trim()) {
  697. if (global_var.type == Types.REAL) {
  698. global_var.value[index] = parseFloat($(this).val().trim());
  699. $(parent_node).find('.span_value_variable').text(global_var.value[index].toFixed(1));
  700. } else {
  701. if (global_var.type == Types.INTEGER) {
  702. global_var.value[index] = parseInt($(this).val().trim());
  703. } else {
  704. global_var.value[index] = $(this).val().trim();
  705. }
  706. $(parent_node).find('.span_value_variable').text(global_var.value[index]);
  707. }
  708. }
  709. $(this).remove();
  710. /// update elements:
  711. opened_name_value_vector_global_ = false;
  712. opened_input_value_vector_global_ = false;
  713. }
  714. if(code == 27) {
  715. if (global_var.type == Types.REAL) {
  716. $(parent_node).find('.span_value_variable').text(global_var.value[index].toFixed(1));
  717. } else {
  718. $(parent_node).find('.span_value_variable').text(global_var.value[index]);
  719. }
  720. $(this).remove();
  721. /// update elements:
  722. opened_name_value_vector_global_ = false;
  723. opened_input_value_vector_global_ = false;
  724. }
  725. });
  726. }
  727. $.fn.textWidth = function(text, font) {
  728. if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
  729. $.fn.textWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));
  730. return $.fn.textWidth.fakeEl.width();
  731. };
  732. /*************************************************
  733. DOUGLAS
  734. **************************************************/
  735. renderGlobal = function (global_var) {
  736. var element = '<div class="ui segment global_container" draggable="true"><div class="global_const">const: ';
  737. 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>';
  738. element += '<div class="ui dropdown global_type">';
  739. if (global_var.dimensions > 0) {
  740. element += '<div class="text">'+ i18n('ui:vector') + ':' + LocalizedStrings.getUI(global_var.type);
  741. for (var i = 0; i < global_var.dimensions; i ++) {
  742. element += ' [ ] ';
  743. }
  744. element += '</div>';
  745. } else {
  746. element += '<div class="text">' + LocalizedStrings.getUI(global_var.type.toLowerCase()) + '</div>';
  747. }
  748. element += '<i class="dropdown icon"></i><div class="menu">';
  749. for (var tm in Types) {
  750. if (tm == Types.VOID.toUpperCase()) {
  751. continue;
  752. }
  753. element += '<div class="item ' + (global_var.type == tm.toLowerCase() ? ' selected ' : '') + '" data-type="'+tm+'" >'+LocalizedStrings.getUI(tm.toLowerCase())+'</div>';
  754. }
  755. for (var tm in Types) {
  756. if (tm == Types.VOID.toUpperCase()) {
  757. continue;
  758. }
  759. element += '<div class="item">'
  760. + '<i class="dropdown icon"></i>'
  761. + LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())
  762. + '<div class="menu">'
  763. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] " data-type="'+tm+'" data-dimensions="1">[ ]</div>'
  764. + '<div class="item" data-text="'+ LocalizedStrings.getUI('vector')+':'+LocalizedStrings.getUI(tm.toLowerCase())+' [ ] [ ] " data-type="'+tm+'" data-dimensions="2">[ ] [ ] </div>'
  765. + '</div>'
  766. + '</div>';
  767. }
  768. element += '</div></div> = ';
  769. element += '<div class="ui div_valor_var">'+global_var.value+'</div>';
  770. element += ' <i class="red icon times remove_global"></i></div>';
  771. var complete_element = $(element);
  772. $(complete_element).data('associatedOject', global_var);
  773. $('.list_globals').append(complete_element);
  774. addHandlers(complete_element);
  775. renderValues(global_var, complete_element);
  776. }