-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve and refactor methods that determine select state.
- Loading branch information
Showing
5 changed files
with
78 additions
and
154 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module TreeNode | ||
module Menu | ||
class Node < TreeNode::Node | ||
set_attribute(:no_click, true) | ||
set_attribute(:checkable) { @options[:editable] } | ||
set_attribute(:selected) { select_state } | ||
|
||
def self_and_child_states | ||
child_states.unshift(select_state) | ||
end | ||
|
||
private | ||
|
||
def child_states | ||
@states ||= children.flat_map(&:self_and_child_states) | ||
end | ||
|
||
def select_by_children_selected | ||
return true if all_children_fully_selected? | ||
return 'undefined' if any_children_selected? | ||
|
||
false | ||
end | ||
|
||
def all_children_fully_selected? | ||
return false if children.empty? | ||
|
||
child_states.all? { |s| s == true } # true not just truthy | ||
end | ||
|
||
def any_children_selected? | ||
return false if children.empty? | ||
|
||
child_states.any? | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,28 @@ | ||
module TreeNode | ||
module Menu | ||
class Section < Node | ||
class Section < TreeNode::Menu::Node | ||
set_attribute(:key) { "#{@options[:node_id_prefix]}___tab_#{@object.id}" } | ||
|
||
set_attribute(:title) { _(@object.name) } | ||
|
||
set_attribute(:image) { "100/feature_node" } | ||
|
||
set_attribute(:checkable) { @options[:editable] } | ||
|
||
set_attribute(:selected) do | ||
select_state_from_descendents | ||
end | ||
|
||
set_attribute(:tooltip) { _("%{title} Main Tab") % {:title => @object.name} } | ||
|
||
set_attribute(:no_click, true) | ||
|
||
private | ||
|
||
def select_state_from_descendents | ||
return false if children.none? | ||
|
||
return true if all_selected? | ||
|
||
return 'undefined' if any_selected? | ||
end | ||
|
||
def all_selected? | ||
return false if children.empty? | ||
|
||
children.all? do |child| | ||
child_selected?(child) | ||
end | ||
end | ||
|
||
def any_selected? | ||
return false if children.empty? | ||
|
||
children.any? do |child| | ||
child_selected?(child) | ||
end | ||
def select_state | ||
select_by_children_selected | ||
end | ||
|
||
def children | ||
@children ||= begin | ||
@object.items.flat_map { |i| collect_children(i) } | ||
@object.items.flat_map do |item| | ||
node = TreeNode.new(item, nil, @options) | ||
[node].concat(node.children) | ||
end | ||
end | ||
end | ||
|
||
def collect_children(item) | ||
if item.kind_of?(::Menu::Section) | ||
item.items.flat_map { |i| collect_children(i) }.unshift(item) | ||
else | ||
item | ||
end | ||
end | ||
|
||
def child_selected?(child) | ||
case child | ||
when ::Menu::Section | ||
child.items.any? { |item| child_selected?(item) } | ||
when ::Menu::Item | ||
TreeNode::Menu::Item.new(child, key, @options).selected? | ||
else | ||
@options[:features].include?(child) | ||
end | ||
end | ||
# End class | ||
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