Skip to content

Commit

Permalink
implemented operator type changing for groups, removed buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Koch committed May 24, 2017
1 parent 66e8e06 commit bb6aa2a
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 28 deletions.
Expand Up @@ -220,7 +220,7 @@
requestData["operand_type"] = "left";
} else if ($(element).hasClass(options.binaryRightOperandClass)) {
requestData["operand_type"] = "right";
}
} else { return }
requestData["operand_value"] = $(element).val();
$.ajax({
url: "/criteria_operator-ui_component/operand_change",
Expand All @@ -237,8 +237,14 @@
requestData["root_operator"] = plugin.$valueInput.val();
requestData["locator"] = plugin.buildLocatorChain(element, options);
requestData["operator_type_value"] = $(element).val();
var type;
if ($(element).hasClass(options.expressionOperatorTypeClass)) {
type = "expression_type_change"
} else if ($(element).hasClass(options.groupOperatorTypeClass)) {
type = "group_type_change"
} else { return }
$.ajax({
url: "/criteria_operator-ui_component/operator_type_change",
url: "/criteria_operator-ui_component/" + type,
data: requestData,
method: "POST"
}).done(function(data) {
Expand Down Expand Up @@ -321,7 +327,9 @@
operandInput: '.criteria_operand_input',
operatorTypeInput: '.criteria_operator_type_input',
binaryLeftOperandClass: 'binary_operator_left_operand',
binaryRightOperandClass: 'binary_operator_right_operand'
binaryRightOperandClass: 'binary_operator_right_operand',
expressionOperatorTypeClass: 'expression_operator_type',
groupOperatorTypeClass: 'group_operator_type'
};

})( jQuery, window, document );
Expand Down
5 changes: 2 additions & 3 deletions app/cells/criteria_operator/ui_component/expression/show.erb
@@ -1,9 +1,8 @@
<div class="criteria_expression_row" data-locator="<%= @locator %>">
<button class="criteria_expression_options_button">...</button>
<input type="text" class="criteria_operand_input binary_operator_left_operand" placeholder="property" value="<%= property_name %>" />
<select class="criteria_operator_type_input">
<select class="criteria_operator_type_input expression_operator_type">
<% operators.each do |op| %>
<option value="<%= op[:value] %>" <% if op[:value] == model.operator_type %>selected<% end %>><%= op[:text] %></option>
<option value="<%= op[:value] %>" <% if op[:value] == operator_type %>selected<% end %>><%= op[:text] %></option>
<% end %>
</select>
<input type="text" class="criteria_operand_input binary_operator_right_operand" placeholder="value" value="<%= value %>" />
Expand Down
8 changes: 8 additions & 0 deletions app/cells/criteria_operator/ui_component/expression_cell.rb
Expand Up @@ -25,6 +25,14 @@ def value
end
end

def operator_type
if model.kind_of?(BinaryOperator)
model.operator_type
else
0
end
end

def operators
ops = []
ops << { value: BinaryOperatorType::EQUAL, text: 'equal to' }
Expand Down
7 changes: 5 additions & 2 deletions app/cells/criteria_operator/ui_component/group/show.erb
@@ -1,6 +1,9 @@
<div class="criteria_group_row" data-locator="<%= @locator %>" data-childcount="<%= @group.operand_collection.count %>">
<button class="criteria_expression_options_button">...</button>
<span class="criteria_group_row_header"><%= caption %></span>
<select class="criteria_operator_type_input group_operator_type">
<% operators.each do |op| %>
<option value="<%= op[:value] %>" <% if op[:value] == operator_type %>selected<% end %>><%= op[:text] %></option>
<% end %>
</select>
<% if @allow_delete %>
<a href="#" class="criteria_group_delete">Delete</a>
<% end %>
Expand Down
31 changes: 14 additions & 17 deletions app/cells/criteria_operator/ui_component/group_cell.rb
Expand Up @@ -17,28 +17,25 @@ def show(options = {})

private

def caption
def operator_type
if model.kind_of? GroupOperator
case model.operator_type
when GroupOperatorType::AND
'AND'
when GroupOperatorType::OR
'OR'
else
'Invalid Group Operator!'
end
model.operator_type
elsif model_negated_group?
case model.operand.operator_type
when GroupOperatorType::AND
'NAND'
when GroupOperatorType::OR
'NOR'
else
'Invalid Group Operator!'
end
model.operand.operator_type * -1
else
0
end
end

def operators
ops = []
ops << { value: GroupOperatorType::AND, text: 'AND' }
ops << { value: GroupOperatorType::OR, text: 'OR' }
ops << { value: -1 * GroupOperatorType::AND, text: 'NAND' }
ops << { value: -1 * GroupOperatorType::OR, text: 'NOR' }
ops
end

def empty?
!@group.kind_of?(GroupOperator) || @group.operand_collection.empty?
end
Expand Down
45 changes: 43 additions & 2 deletions app/controllers/criteria_operator/ui_component/ajax_controller.rb
Expand Up @@ -40,14 +40,21 @@ def operand_change
render json: { operator: YAML.dump(root_operator) }
end

def operator_type_change
def expression_type_change
return unless (ajax_params.has_key? :root_operator) && (ajax_params.has_key? :locator)
root_operator = root_op_from_params
op = locate_sub_operator root_operator, ajax_params[:locator]
op.operator_type = ajax_params[:operator_type_value]
render json: { operator: YAML.dump(root_operator) }
end

def group_type_change
return unless (ajax_params.has_key? :root_operator) && (ajax_params.has_key? :locator)
root_operator = root_op_from_params
root_operator = change_sub_group_type root_operator, ajax_params[:locator], ajax_params[:operator_type_value].to_i
render json: { operator: YAML.dump(root_operator) }
end

private

def ajax_params
Expand Down Expand Up @@ -93,13 +100,47 @@ def remove_sub_operator(root_op, locator)
op.operand_collection.delete_at pos.to_i
end

def change_sub_group_type(root_op, locator, op_type)
op = root_op
last_pos = -1
parent_op = nil
locator_array = locator.split(',')
locator_array.map(&:to_i).each do |pos|
op = get_negated_group_if_exist op
last_pos = pos
parent_op = op
op = op.operand_collection[pos]
end

if negation? op
if op_type > 0
parent_op.operand_collection[last_pos] = op.operand
op.operand.operator_type = op_type
else
op.operand.operator_type = op_type * -1
end
else
if op_type < 0
parent_op.operand_collection[last_pos] = UnaryOperator.new op, UnaryOperatorType::NOT
op.operator_type = op_type * -1
else
op.operator_type = op_type
end
end
root_op
end

def get_negated_group_if_exist(op)
if op.is_a?(UnaryOperator) && (op.operator_type == UnaryOperatorType::NOT)
if negation? op
op.operand
else
op
end
end

def negation?(op)
op.is_a?(UnaryOperator) && (op.operator_type == UnaryOperatorType::NOT)
end
end
end
end
3 changes: 2 additions & 1 deletion config/routes.rb
Expand Up @@ -3,5 +3,6 @@
post 'create_group', controller: :ajax, action: :create_group
post 'delete_element', controller: :ajax, action: :delete_element
post 'operand_change', controller: :ajax, action: :operand_change
post 'operator_type_change', controller: :ajax, action: :operator_type_change
post 'expression_type_change', controller: :ajax, action: :expression_type_change
post 'group_type_change', controller: :ajax, action: :group_type_change
end

0 comments on commit bb6aa2a

Please sign in to comment.