attribution.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. import * as Models from '../ivprog_elements';
  2. import { LocalizedStrings } from '../../services/localizedStringsService';
  3. import * as VariableValueMenu from './variable_value_menu';
  4. import * as VariableValueMenuManagement from './variable_value_menu';
  5. import * as CommandsManagement from '../commands';
  6. import * as ExpressionManagement from './generic_expression';
  7. import * as CodeGenerator from '../code_generator';
  8. export function createFloatingCommand () {
  9. return $('<div class="ui attribution created_element"> <i class="ui icon small arrow left"></i> <span> x <&#8212; 1 + 1 </span></div>');
  10. }
  11. export function renderCommand (command, function_obj) {
  12. var el = $('<div class="ui attribution command_container"><i class="ui icon small arrow left command_drag"></i> <i class="ui icon times red button_remove_command"></i> <div class="var_attributed"></div> <span class="text_attr_receives span_command_spec">'+LocalizedStrings.getUI('text_receives')+'</span> '
  13. + '<div class="expression_elements"></div> <span class="textual_expression"></span> <i class="ui icon unlock button_alternate_expression"></i> </div>');
  14. el.data('command', command);
  15. VariableValueMenu.renderMenu(command, command.variable, el.find('.var_attributed'), function_obj);
  16. if (!command.expression || command.expression.length < 1) {
  17. var exp = new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true);
  18. command.expression.push(exp);
  19. }
  20. addHandlers(command, function_obj, el);
  21. ExpressionManagement.renderExpression(command, function_obj, el.find('.expression_elements'), command.expression);
  22. if (command.lockexpression) {
  23. if (command.expression) {
  24. try {
  25. var text = CodeGenerator.elementExpressionCode(command.expression);
  26. if (text) {
  27. $(el.find('.expression_elements')[0]).toggle();
  28. $(el.find('.textual_expression')[0]).text(text);
  29. $(el.find('.textual_expression')[0]).toggle();
  30. $(el.find('.button_alternate_expression')[0]).toggleClass('unlock').toggleClass('lock');
  31. }
  32. } catch (e) {
  33. command.lockexpression = false;
  34. }
  35. }
  36. }
  37. return el;
  38. }
  39. export function manageExpressionElements (command, ref_object, dom_object, menu_var_or_value, function_obj, selectedItem, expression_element) {
  40. var index_to_move = expression_element.itens.indexOf(ref_object);
  41. switch (selectedItem.data('exp')) {
  42. case Models.EXPRESSION_ELEMENTS.exp_op_exp:
  43. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp, [expression_element.itens[index_to_move],
  44. Models.ARITHMETIC_TYPES.plus,
  45. new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  46. expression_element.itens[index_to_move] = exp;
  47. break;
  48. case Models.EXPRESSION_ELEMENTS.op_exp:
  49. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.op_exp, [Models.ARITHMETIC_TYPES.plus,
  50. expression_element.itens[index_to_move] ]);
  51. expression_element.itens[index_to_move] = exp;
  52. break;
  53. case Models.EXPRESSION_ELEMENTS.par_exp_par:
  54. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.par_exp_par, [expression_element.itens[index_to_move]]);
  55. expression_element.itens[index_to_move] = exp;
  56. break;
  57. }
  58. renderExpressionElements(command, function_obj, dom_object);
  59. }
  60. function renderExpressionElements (command, function_obj, el) {
  61. var expression_div = el.find('.expression_elements');
  62. var command_container;
  63. if (el.hasClass("command_container") == false) {
  64. var hier = el.parentsUntil(".command_container");
  65. for (var i = 0; i < hier.length; i++) {
  66. if ($(hier[i]).hasClass("command_container")) {
  67. command_container = $(hier[i]);
  68. break;
  69. }
  70. if ($(hier[i]).hasClass("expression_elements")) {
  71. expression_div = $(hier[i]);
  72. break;
  73. }
  74. }
  75. }
  76. if (command_container) {
  77. expression_div = command_container.find('.expression_elements');
  78. }
  79. expression_div.text('');
  80. for (var i = 0; i < command.expression.length; i++) {
  81. var temp = $('<div class="expression_element"></div>');
  82. temp.data('ref_element', command.expression[i]);
  83. temp.data('ref_index', i);
  84. expression_div.append(temp);
  85. renderElement(command, function_obj, temp, command.expression[i]);
  86. }
  87. }
  88. function renderOperator (command, function_obj, temp_op, expression_element, index_op) {
  89. var context_menu = '<div class="ui dropdown"><div class="text">';
  90. switch (expression_element.itens[index_op]) {
  91. case Models.ARITHMETIC_TYPES.plus:
  92. context_menu += '+';
  93. break;
  94. case Models.ARITHMETIC_TYPES.minus:
  95. context_menu += '-';
  96. break;
  97. case Models.ARITHMETIC_TYPES.multiplication:
  98. context_menu += '*';
  99. break;
  100. case Models.ARITHMETIC_TYPES.division:
  101. context_menu += '/';
  102. break;
  103. case Models.ARITHMETIC_TYPES.module:
  104. context_menu += '%';
  105. break;
  106. case Models.ARITHMETIC_TYPES.none:
  107. context_menu += '...';
  108. break;
  109. }
  110. context_menu += '</div><div class="menu">';
  111. context_menu += '<div class="item" data-value="'+Models.ARITHMETIC_TYPES.plus+'">+</div>';
  112. context_menu += '<div class="item" data-value="'+Models.ARITHMETIC_TYPES.minus+'">-</div>';
  113. context_menu += '<div class="item" data-value="'+Models.ARITHMETIC_TYPES.multiplication+'">*</div>';
  114. context_menu += '<div class="item" data-value="'+Models.ARITHMETIC_TYPES.division+'">/</div>';
  115. context_menu += '<div class="item" data-value="'+Models.ARITHMETIC_TYPES.module+'">%</div>';
  116. context_menu += '<div class="item" data-value="'+Models.ARITHMETIC_TYPES.none+'" data-text="...">Nenhum</div>';
  117. context_menu += '</div></div>';
  118. context_menu = $(context_menu);
  119. temp_op.append(context_menu);
  120. context_menu.dropdown({
  121. onChange: function(value, text, $selectedItem) {
  122. expression_element.itens[index_op] = value;
  123. }
  124. });
  125. }
  126. function renderMenuAddExpression (command, function_obj, el, dom_append_menu, expression_append_new_expression) {
  127. if (el.hasClass("command_container") == false) {
  128. var hier = el.parentsUntil(".commands_list_div");
  129. for (var i = 0; i < hier.length; i++) {
  130. if ($(hier[i]).hasClass("command_container")) {
  131. el = $(hier[i]);
  132. break;
  133. }
  134. }
  135. }
  136. if (dom_append_menu.hasClass("expression_elements") == false) {
  137. var hier = el.parentsUntil(".commands_list_div");
  138. for (var i = 0; i < hier.length; i++) {
  139. if ($(hier[i]).hasClass("expression_elements")) {
  140. dom_append_menu = $(hier[i]);
  141. break;
  142. }
  143. }
  144. }
  145. var context_menu = '<div class="ui dropdown"><div class="text"></div><i class="ui icon arrow alternate circle right outline"></i><div class="menu">';
  146. context_menu += '<div class="item" data-value="'+Models.EXPRESSION_ELEMENTS.exp_op_exp+'">EXP OP EXP</div>';
  147. context_menu += '<div class="item" data-value="'+Models.EXPRESSION_ELEMENTS.op_exp+'">OP EXP</div>';
  148. context_menu += '<div class="item" data-value="'+Models.EXPRESSION_ELEMENTS.par_exp_par+'">( EXP )</div>';
  149. context_menu += '</div></div>';
  150. context_menu = $(context_menu);
  151. dom_append_menu.append(context_menu);
  152. context_menu.dropdown({
  153. onChange: function(value, text, $selectedItem) {
  154. switch (value) {
  155. case Models.EXPRESSION_ELEMENTS.exp_op_exp:
  156. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp, [new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true),
  157. Models.ARITHMETIC_TYPES.plus,
  158. new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  159. expression_append_new_expression.push(exp);
  160. break;
  161. case Models.EXPRESSION_ELEMENTS.op_exp:
  162. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.op_exp, [Models.ARITHMETIC_TYPES.plus,
  163. new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  164. expression_append_new_expression.push(exp);
  165. break;
  166. case Models.EXPRESSION_ELEMENTS.par_exp_par:
  167. var exp = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.par_exp_par, [new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true)]);
  168. expression_append_new_expression.push(exp);
  169. break;
  170. }
  171. renderExpressionElements(command, function_obj, el);
  172. }
  173. });
  174. }
  175. function renderElement (command, function_obj, el, expression_element) {
  176. switch (expression_element.type_exp) {
  177. case Models.EXPRESSION_ELEMENTS.exp_op_exp:
  178. var temp_op = $('<div class="component_element"></div>');
  179. var temp_exp_1 = $('<div class="component_element"></div>');
  180. var temp_exp_2 = $('<div class="component_element"></div>');
  181. el.append(temp_exp_1);
  182. el.append(temp_op);
  183. el.append(temp_exp_2);
  184. if (expression_element.itens[0].type) {
  185. VariableValueMenu.renderMenu(command, expression_element.itens[0], temp_exp_1, function_obj, 2, expression_element);
  186. } else {
  187. renderElement(command, function_obj, temp_exp_1, expression_element.itens[0]);
  188. }
  189. renderOperator(command, function_obj, temp_op, expression_element, 1);
  190. if (expression_element.itens[2].type) {
  191. VariableValueMenu.renderMenu(command, expression_element.itens[2], temp_exp_2, function_obj, 2, expression_element);
  192. } else {
  193. renderElement(command, function_obj, temp_exp_2, expression_element.itens[2]);
  194. }
  195. break;
  196. case Models.EXPRESSION_ELEMENTS.op_exp:
  197. var temp_op = $('<div class="component_element"></div>');
  198. var temp_exp = $('<div class="component_element"></div>');
  199. el.append(temp_op);
  200. el.append(temp_exp);
  201. renderOperator(command, function_obj, temp_op, expression_element, 0);
  202. if (expression_element.itens[1].type) {
  203. VariableValueMenu.renderMenu(command, expression_element.itens[1], temp_exp, function_obj, 2, expression_element);
  204. } else {
  205. renderElement(command, function_obj, temp_exp, expression_element.itens[1]);
  206. }
  207. break;
  208. case Models.EXPRESSION_ELEMENTS.par_exp_par:
  209. var temp_par_1 = $('<div class="component_element"> ( </div>');
  210. var temp_exp = $('<div class="component_element"></div>');
  211. var temp_par_2 = $('<div class="component_element"> ) </div>');
  212. el.append(temp_par_1);
  213. el.append(temp_exp);
  214. for (var j = 0; j < expression_element.itens.length; j++) {
  215. if (expression_element.itens[j].type) {
  216. VariableValueMenu.renderMenu(command, expression_element.itens[j], temp_exp, function_obj, 2, expression_element);
  217. } else {
  218. renderElement(command, function_obj, temp_exp, expression_element.itens[j]);
  219. }
  220. }
  221. //renderMenuAddExpression(command, function_obj, el, el, expression_element.itens);
  222. el.append(temp_par_2);
  223. break;
  224. }
  225. }
  226. function renderExpression (command, function_obj, el) {
  227. var expression_div = el.find('.expression_elements');
  228. expression_div.text('');
  229. var menu_add_item = $('<div class="menu_add_item"></div>');
  230. menu_add_item.data('index_add', 0);
  231. expression_div.append(menu_add_item);
  232. for (var i = 0; i < command.expression.length; i++) {
  233. if (command.expression[i].type) {
  234. var temp = $('<div class="expression_element"></div>');
  235. temp.data('ref_element', command.expression[i]);
  236. temp.data('ref_index', i);
  237. expression_div.append(temp);
  238. VariableValueMenu.renderMenu(command, command.expression[i], temp, function_obj);
  239. } else if (command.expression[i] == "(" || command.expression[i] == ")") {
  240. var temp = $('<div class="expression_element">'+command.expression[i]+'</div>');
  241. temp.data('ref_element', command.expression[i]);
  242. temp.data('ref_index', i);
  243. expression_div.append(temp);
  244. } else {
  245. var temp = '<div class="expression_element">';
  246. switch(command.expression[i]) {
  247. case Models.ARITHMETIC_TYPES.plus:
  248. temp += '+';
  249. break;
  250. case Models.ARITHMETIC_TYPES.minus:
  251. temp += '-';
  252. break;
  253. case Models.ARITHMETIC_TYPES.multiplication:
  254. temp += '*';
  255. break;
  256. case Models.ARITHMETIC_TYPES.division:
  257. temp += '/';
  258. break;
  259. case Models.ARITHMETIC_TYPES.module:
  260. temp += '%';
  261. break;
  262. }
  263. temp += '</div>';
  264. temp = $(temp);
  265. temp.data('ref_element', command.expression[i]);
  266. temp.data('ref_index', i);
  267. expression_div.append(temp);
  268. }
  269. var menu_add_item_seq = $('<div class="menu_add_item"></div>');
  270. var index_temp = (i + 1);
  271. menu_add_item_seq.data('index_add', index_temp);
  272. expression_div.append(menu_add_item_seq);
  273. }
  274. addMenuItens(command, function_obj, el);
  275. }
  276. function addMenuItens (command, function_obj, expression_div) {
  277. var divs_expression = expression_div.find('.menu_add_item');
  278. for (var i = 0; i < divs_expression.length; i++) {
  279. var temp = $(divs_expression[i]).data('index_add');
  280. var context_menu = '<div class="ui dropdown context_menu_clear"><i class="ui icon plus square outline"></i><div class="menu">';
  281. context_menu += '<div class="item" data-option="value" data-index="'+temp+'">'+LocalizedStrings.getUI('text_value')+'</div>';
  282. context_menu += '<div class="item" data-option="operator" data-index="'+temp+'">'+LocalizedStrings.getUI('text_operator')+'</div>';
  283. context_menu += '<div class="item" data-option="parentheses" data-index="'+temp+'">'+LocalizedStrings.getUI('text_parentheses')+'</div>';
  284. context_menu += '</div></div>';
  285. context_menu = $(context_menu);
  286. $(divs_expression[i]).append(context_menu);
  287. context_menu.dropdown({
  288. on: 'hover',
  289. onChange: function(value, text, $selectedItem) {
  290. switch ($selectedItem.data('option')) {
  291. case "value":
  292. command.expression.splice($selectedItem.data('index'), 0, new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true));
  293. renderExpression(command, function_obj, expression_div);
  294. break;
  295. case "operator":
  296. command.expression.splice($selectedItem.data('index'), 0, Models.ARITHMETIC_TYPES.plus);
  297. renderExpression(command, function_obj, expression_div);
  298. break;
  299. case "parentheses":
  300. command.expression.splice($selectedItem.data('index'), 0, "(");
  301. command.expression.splice($selectedItem.data('index') + 1, 0, new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true));
  302. command.expression.splice($selectedItem.data('index') + 2, 0, ")");
  303. renderExpression(command, function_obj, expression_div);
  304. break;
  305. }
  306. }
  307. });
  308. }
  309. }
  310. function addHandlers (command, function_obj, attribution_dom) {
  311. $(attribution_dom.find('.textual_expression')[0]).toggle();
  312. attribution_dom.find('.button_alternate_expression').on('click', function() {
  313. if (command.expression) {
  314. var text = CodeGenerator.elementExpressionCode(command.expression);
  315. if (text) {
  316. $(attribution_dom.find('.expression_elements')[0]).toggle();
  317. $(attribution_dom.find('.textual_expression')[0]).text(text);
  318. $(attribution_dom.find('.textual_expression')[0]).toggle();
  319. $(this).toggleClass('unlock').toggleClass('lock');
  320. command.lockexpression = !command.lockexpression;
  321. }
  322. }
  323. });
  324. attribution_dom.find('.button_remove_command').on('click', function() {
  325. if (CommandsManagement.removeCommand(command, function_obj, attribution_dom)) {
  326. attribution_dom.fadeOut(400, function() {
  327. attribution_dom.remove();
  328. });
  329. }
  330. });
  331. attribution_dom.find('.button_refresh_attribution').on('click', function() {
  332. renderExpressionElements(command, function_obj, attribution_dom);
  333. });
  334. }
  335. export function renderMenuOperations (command, ref_object, dom_object, menu_var_or_value, function_obj, variable_selected) {
  336. /*console.log("recebido o seguinte DOM: ");
  337. console.log(dom_object);
  338. if (dom_object.hasClass('var_attributed')) {
  339. return;
  340. } else {
  341. var hier = dom_object.parentsUntil(".command_container");
  342. for (var i = 0; i < hier.length; i++) {
  343. if ($(hier[i]).hasClass('var_attributed') || $(hier[i]).hasClass('parameters_function_called')) {
  344. return;
  345. }
  346. }
  347. }
  348. dom_object.find('.context_menu_clear').remove();
  349. var menu_operations = '<div class="ui dropdown menu_operations"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  350. for (var tm in Models.ARITHMETIC_TYPES) {
  351. menu_operations += '<div class="item" data-option="'+tm+'">'+LocalizedStrings.getUI('btn_arithmetic_' + tm)+'</div>';
  352. }
  353. menu_operations += '<div class="item" data-option="clear">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  354. menu_operations += '</div></div>';
  355. menu_operations = $(menu_operations);
  356. dom_object.append(menu_operations);
  357. menu_operations.dropdown({
  358. onChange: function(value, text, $selectedItem) {
  359. switch ($($selectedItem).data('option')) {
  360. case "clear":
  361. $(dom_object).text('');
  362. VariableValueMenu.renderMenu(command, ref_object, dom_object, function_obj);
  363. break;
  364. default:
  365. createExpressionAround(command, ref_object, dom_object, function_obj);
  366. menu_operations.find('.text').text('');
  367. }
  368. }
  369. });*/
  370. }
  371. function createExpressionAround (command, ref_object, dom_object, function_obj) {
  372. $('<span> ( </span>').insertBefore(dom_object);
  373. $('<span> ) </span>').insertAfter(dom_object);
  374. VariableValueMenu.renderMenu(command, new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true), dom_object, function_obj);
  375. }