attribution.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import $ from 'jquery';
  2. import { Types } from '../types';
  3. import * as Models from '../ivprog_elements';
  4. import { LocalizedStrings } from '../../services/localizedStringsService';
  5. import * as GlobalsManagement from '../globals';
  6. import * as VariablesManagement from '../variables';
  7. import * as VariableValueMenu from './variable_value_menu';
  8. import * as VariableValueMenuManagement from './variable_value_menu';
  9. import * as CommandsManagement from '../commands';
  10. export function createFloatingCommand () {
  11. return $('<div class="ui attribution created_element"> <i class="ui icon small arrow left"></i> <span> x = 1 + 1 </span></div>');
  12. }
  13. export function renderCommand (command, function_obj) {
  14. 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">'+LocalizedStrings.getUI('text_receives')+'</span> '
  15. + '<div class="expression_operand_1"></div> </div>');
  16. el.data('command', command);
  17. VariableValueMenu.renderMenu(command, command.variable, el.find('.var_attributed'), function_obj);
  18. command.expression[0].content = new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true);
  19. VariableValueMenu.renderMenu(command, command.expression[0].content, el.find('.expression_operand_1'), function_obj);
  20. addHandlers(command, function_obj, el);
  21. return el;
  22. /*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">'+LocalizedStrings.getUI('text_receives')+'</span> '
  23. + '<div class="expression_operand_1"></div> </div>');
  24. $(el).data('command', command);
  25. VariableValueMenu.renderMenu(command, command.variable, $(el).find('.var_attributed'), function_obj);
  26. VariableValueMenu.renderMenu(command, command.expression, $(el).find('.expression_operand_1'), function_obj);
  27. addHandlers(command, function_obj, el);
  28. return el;*/
  29. }
  30. function addHandlers (command, function_obj, attribution_dom) {
  31. attribution_dom.find('.button_remove_command').on('click', function() {
  32. if (CommandsManagement.removeCommand(command, function_obj, attribution_dom)) {
  33. attribution_dom.remove();
  34. }
  35. });
  36. }
  37. export function renderMenuOperations (command, ref_object, dom_object, menu_var_or_value, function_obj, variable_selected) {
  38. console.log("recebido o seguinte DOM: ");
  39. console.log(dom_object);
  40. if (dom_object.hasClass('var_attributed')) {
  41. return;
  42. } else {
  43. var hier = dom_object.parentsUntil(".command_container");
  44. for (var i = 0; i < hier.length; i++) {
  45. if ($(hier[i]).hasClass('var_attributed') || $(hier[i]).hasClass('parameters_function_called')) {
  46. return;
  47. }
  48. }
  49. }
  50. dom_object.find('.context_menu_clear').remove();
  51. var menu_operations = '<div class="ui dropdown menu_operations"><div class="text"></div><i class="dropdown icon"></i><div class="menu">';
  52. for (var tm in Models.ARITHMETIC_TYPES) {
  53. menu_operations += '<div class="item" data-option="'+tm+'">'+LocalizedStrings.getUI('btn_arithmetic_' + tm)+'</div>';
  54. }
  55. menu_operations += '<div class="item" data-option="clear">'+LocalizedStrings.getUI('btn_clear')+'</div>';
  56. menu_operations += '</div></div>';
  57. menu_operations = $(menu_operations);
  58. dom_object.append(menu_operations);
  59. menu_operations.dropdown({
  60. onChange: function(value, text, $selectedItem) {
  61. switch ($($selectedItem).data('option')) {
  62. case "clear":
  63. $(dom_object).text('');
  64. VariableValueMenu.renderMenu(command, ref_object, dom_object, function_obj);
  65. break;
  66. default:
  67. createExpressionAround(command, ref_object, dom_object, function_obj);
  68. menu_operations.find('.text').text('');
  69. }
  70. }
  71. });
  72. }
  73. function createExpressionAround (command, ref_object, dom_object, function_obj) {
  74. $('<span> ( </span>').insertBefore(dom_object);
  75. $('<span> ) </span>').insertAfter(dom_object);
  76. VariableValueMenu.renderMenu(command, new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all, null, null, null, true), dom_object, function_obj);
  77. }