2016-09-25 23:19:06 +00:00
|
|
|
# helper methods for source/layouts/sidebar.slim
|
|
|
|
module SidebarHelpers
|
|
|
|
SIDEBAR_LAYOUTS = %w{docs}.freeze
|
|
|
|
|
|
|
|
def sidebar_data(sidebar_layout)
|
|
|
|
unless SIDEBAR_LAYOUTS.include?(sidebar_layout)
|
2017-02-08 22:49:16 +00:00
|
|
|
raise "'#{sidebar_layout}' is not a valid sidebar layout type."
|
2016-09-25 23:19:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
data.public_send(:"#{sidebar_layout}_sidebar").sidebar_links.dup
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_classes(current_url, item_link)
|
2019-06-11 22:24:35 +00:00
|
|
|
"t-purple" if same_link?(current_url, item_link.link)
|
2016-09-25 23:19:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def print_sub_links?(current_url, item_link)
|
|
|
|
return false unless sub_links?(item_link)
|
|
|
|
|
|
|
|
same_link?(item_link.link, current_url) ||
|
|
|
|
active_child?(current_url, item_link)
|
|
|
|
end
|
|
|
|
|
|
|
|
def same_link?(one, two)
|
2018-02-17 15:44:42 +00:00
|
|
|
# fix comparing '.html' to empty suffix links
|
2019-06-11 22:24:35 +00:00
|
|
|
if !one.end_with?(".html") && two.end_with?(".html")
|
|
|
|
two = two.sub(/\.html$/, "")
|
2018-02-17 15:44:42 +00:00
|
|
|
end
|
2016-09-25 23:19:06 +00:00
|
|
|
strip_trailing_slash(one) == strip_trailing_slash(two)
|
|
|
|
end
|
|
|
|
|
|
|
|
def strip_trailing_slash(str)
|
2019-06-11 22:24:35 +00:00
|
|
|
str.end_with?("/") ? str[0..-2] : str
|
2016-09-25 23:19:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def active_child?(current_url, item_link)
|
|
|
|
return false unless sub_links?(item_link)
|
|
|
|
|
|
|
|
sub_link_urls(item_link).include?(strip_trailing_slash(current_url))
|
|
|
|
end
|
|
|
|
|
|
|
|
def sub_links?(item_link)
|
|
|
|
item_link.respond_to?(:sub_links) && item_link.sub_links.count > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def sub_link_urls(item)
|
|
|
|
item.sub_links.collect { |sub| strip_trailing_slash(sub.link) }
|
|
|
|
end
|
|
|
|
end
|