1
0

globals_sidebar.js 29 KB

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