-
Notifications
You must be signed in to change notification settings - Fork 356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom buttons for list views #796
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
5fd1d21
Add custom button options for list view support.
martinpovolny 807b3b1
Toolbar: submit list of checked items for custom buttons.
martinpovolny 4b5a8d3
Enable custom button toolbar for list views.
martinpovolny 7e24e68
Add list support to custom button toolbar/button creation.
martinpovolny 7328d73
Add list support to custom button click action handling.
martinpovolny 63c28d6
Replace find_by_id with find to make Rubocop happy.
martinpovolny 59abac3
Fix custom_button specs to match added arguments.
martinpovolny 4afe315
Differenciate custom buttons for lists and single item.
martinpovolny 7ad4907
Custom buttons: temporary disable submit_how:all.
martinpovolny 7b571ec
Custom buttons: remove forgotten "pry".
martinpovolny 4d6656c
Custom buttons: handle situation where @view_context is a controller.
martinpovolny 802d3bb
Custom buttons: display custom buttons for lists when navigating a tree.
martinpovolny 29ddceb
Custom buttons: make Rubocop less unhappy.
martinpovolny 6a7e18d
Custom buttons: add missing file.
martinpovolny 0ef724a
Custom buttons: Fix specs.
martinpovolny 6834d56
CustomButtons: rename result class.
martinpovolny 2f087b5
CustomButtons: remove question marks from custom_toolbar*?.
martinpovolny 0a95184
Custom buttons: fixes for zero items and default submit type.
martinpovolny 7816308
CustomToolbar: for plural case the record class needs to be taken fro…
martinpovolny 19f4fe9
CustomButtons: rubocop fixes.
martinpovolny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Mixins::CustomButtons | ||
Result = Struct.new(:plural_form) do | ||
def plural_form_matches(button) | ||
if plural_form == :list | ||
%w(list both).include?(button.options.try(:[], :display_for)) | ||
|
||
else # :single | ||
[nil, 'single', 'both'].include?(button.options.try(:[], :display_for)) | ||
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,11 +54,19 @@ def predefined_toolbar_class(tb_name) | |
class_name.constantize | ||
end | ||
|
||
def controller | ||
@view_context.respond_to?(:controller) ? @view_context.controller : @view_context | ||
end | ||
|
||
def model_for_custom_toolbar | ||
controller.instance_eval { @tree_selected_model } || controller.class.model | ||
end | ||
|
||
# According to toolbar name in parameter `toolbar_name` either returns class | ||
# for generic toolbar, or starts building custom toolbar | ||
def toolbar_class(toolbar_name) | ||
if toolbar_name == "custom_buttons_tb" | ||
custom_toolbar_class(@record) | ||
if Mixins::CustomButtons::Result === toolbar_name | ||
custom_toolbar_class(toolbar_name) | ||
else | ||
predefined_toolbar_class(toolbar_name) | ||
end | ||
|
@@ -227,10 +235,11 @@ def group_skipped?(name) | |
x_edit_view_tb history_main ems_container_dashboard ems_infra_dashboard).include?(name) | ||
end | ||
|
||
def create_custom_button_hash(input, record, options = {}) | ||
def create_custom_button(input, model, record, options = {}) | ||
options[:enabled] = true unless options.key?(:enabled) | ||
button_id = input[:id] | ||
button_name = input[:name].to_s | ||
record_id = record.present? ? record.id : 'LIST' | ||
button = { | ||
:id => "custom__custom_#{button_id}", | ||
:type => :button, | ||
|
@@ -239,62 +248,73 @@ def create_custom_button_hash(input, record, options = {}) | |
:enabled => options[:enabled], | ||
:klass => ApplicationHelper::Button::ButtonWithoutRbacCheck, | ||
:url => "button", | ||
:url_parms => "?id=#{record.id}&button_id=#{button_id}&cls=#{record.class}&pressed=custom_button&desc=#{button_name}" | ||
:url_parms => "?id=#{record_id}&button_id=#{button_id}&cls=#{model}&pressed=custom_button&desc=#{button_name}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
button[:text] = button_name if input[:text_display] | ||
button | ||
end | ||
|
||
def create_raw_custom_button_hash(cb, record) | ||
record_id = record.present? ? record.id : 'LIST' | ||
{ | ||
:id => cb.id, | ||
:class => cb.applies_to_class, | ||
:description => cb.description, | ||
:name => cb.name, | ||
:image => cb.options[:button_image], | ||
:text_display => cb.options.key?(:display) ? cb.options[:display] : true, | ||
:target_object => record.id.to_i | ||
:target_object => record_id | ||
} | ||
end | ||
|
||
def custom_buttons_hash(record) | ||
get_custom_buttons(record).collect do |group| | ||
def custom_button_selects(model, record, toolbar_result) | ||
get_custom_buttons(model, record, toolbar_result).collect do |group| | ||
props = { | ||
:id => "custom_#{group[:id]}", | ||
:type => :buttonSelect, | ||
:icon => "product product-custom-#{group[:image]} fa-lg", | ||
:title => group[:description], | ||
:enabled => true, | ||
:items => group[:buttons].collect { |b| create_custom_button_hash(b, record) } | ||
:items => group[:buttons].collect { |b| create_custom_button(b, model, record) } | ||
} | ||
props[:text] = group[:text] if group[:text_display] | ||
|
||
{:name => "custom_buttons_#{group[:text]}", :items => [props]} | ||
end | ||
end | ||
|
||
def custom_toolbar_class(record) | ||
def custom_toolbar_class(toolbar_result) | ||
model = @record ? @record.class : model_for_custom_toolbar | ||
build_custom_toolbar_class(model, @record, toolbar_result) | ||
end | ||
|
||
def build_custom_toolbar_class(model, record, toolbar_result) | ||
# each custom toolbar is an anonymous subclass of this class | ||
|
||
toolbar = Class.new(ApplicationHelper::Toolbar::Basic) | ||
custom_buttons_hash(record).each do |button_group| | ||
|
||
# This creates several drop-down (select) with custom buttons. | ||
# Each select is placed into a separate group. | ||
custom_button_selects(model, record, toolbar_result).each do |button_group| | ||
toolbar.button_group(button_group[:name], button_group[:items]) | ||
end | ||
|
||
service_buttons = record_to_service_buttons(record) | ||
unless service_buttons.empty? | ||
buttons = service_buttons.collect { |b| create_custom_button_hash(b, record, :enabled => nil) } | ||
toolbar.button_group("custom_buttons_", buttons) | ||
# For Service, we include buttons for ServiceTemplate. | ||
# These buttons are added as a single group with multiple buttons | ||
if record.present? | ||
service_buttons = record_to_service_buttons(record) | ||
unless service_buttons.empty? | ||
buttons = service_buttons.collect { |b| create_custom_button(b, model, record, :enabled => nil) } | ||
toolbar.button_group("custom_buttons_", buttons) | ||
end | ||
end | ||
|
||
toolbar | ||
end | ||
|
||
def button_class_name(record) | ||
case record | ||
when Service then "ServiceTemplate" # Service Buttons are defined in the ServiceTemplate class | ||
when VmOrTemplate then record.class.base_model.name | ||
else record.class.base_class.name | ||
end | ||
def button_class_name(model) | ||
# Service Buttons are defined in the ServiceTemplate class | ||
model >= Service ? "ServiceTemplate" : model.base_model.name | ||
end | ||
|
||
def service_template_id(record) | ||
|
@@ -310,9 +330,9 @@ def record_to_service_buttons(record) | |
record.service_template.custom_buttons.collect { |cb| create_raw_custom_button_hash(cb, record) } | ||
end | ||
|
||
def get_custom_buttons(record) | ||
cbses = CustomButtonSet.find_all_by_class_name(button_class_name(record), service_template_id(record)) | ||
cbses.sort_by { |cbs| cbs[:set_data][:group_index] }.collect do |cbs| | ||
def get_custom_buttons(model, record, toolbar_result) | ||
cbses = CustomButtonSet.find_all_by_class_name(button_class_name(model), service_template_id(record)) | ||
cbses.sort_by { |cbs| cbs.set_data[:group_index] }.collect do |cbs| | ||
group = { | ||
:id => cbs.id, | ||
:text => cbs.name.split("|").first, | ||
|
@@ -322,11 +342,14 @@ def get_custom_buttons(record) | |
} | ||
|
||
available = CustomButton.available_for_user(current_user, cbs.name) # get all uri records for this user for specified uri set | ||
available = available.select { |b| cbs.members.include?(b) } # making sure available_for_user uri is one of the members | ||
available = available.select do |b| | ||
cbs.members.include?(b) && toolbar_result.plural_form_matches(b) | ||
end | ||
|
||
group[:buttons] = available.collect { |cb| create_raw_custom_button_hash(cb, record) }.uniq | ||
if cbs[:set_data][:button_order] # Show custom buttons in the order they were saved | ||
if cbs.set_data[:button_order] # Show custom buttons in the order they were saved | ||
ordered_buttons = [] | ||
cbs[:set_data][:button_order].each do |bidx| | ||
cbs.set_data[:button_order].each do |bidx| | ||
group[:buttons].each do |b| | ||
if bidx == b[:id] && !ordered_buttons.include?(b) | ||
ordered_buttons.push(b) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vecerek You will need to handle this case in #642 :)
(The id=LIST is needed by backend, but doesn't have to imply miq_grid_checks in general, just for the buttons touched here..)