globals.js 29 KB

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