/usr/lib/ruby/vendor_ruby/simple_navigation/item.rb is in ruby-simple-navigation 4.0.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | module SimpleNavigation
# Represents an item in your navigation.
# Gets generated by the item method in the config-file.
class Item
attr_reader :key,
:name,
:sub_navigation,
:url
# see ItemContainer#item
#
# The subnavigation (if any) is either provided by a block or
# passed in directly as <tt>items</tt>
def initialize(container, key, name, url = nil, opts = {}, &sub_nav_block)
self.container = container
self.key = key
self.name = name.respond_to?(:call) ? name.call : name
self.url = url.respond_to?(:call) ? url.call : url
self.options = opts
setup_sub_navigation(options[:items], &sub_nav_block)
end
# Returns the item's name.
# If :apply_generator option is set to true (default),
# the name will be passed to the name_generator specified
# in the configuration.
#
def name(options = {})
options = { apply_generator: true }.merge(options)
if options[:apply_generator]
config.name_generator.call(@name, self)
else
@name
end
end
# Returns true if this navigation item should be rendered as 'selected'.
# An item is selected if
#
# * it has a subnavigation and one of its subnavigation items is selected or
# * its url matches the url of the current request (auto highlighting)
#
def selected?
@selected ||= selected_by_subnav? || selected_by_condition?
end
# Returns the html-options hash for the item, i.e. the options specified
# for this item in the config-file.
# It also adds the 'selected' class to the list of classes if necessary.
def html_options
html_opts = options.fetch(:html) { Hash.new }
html_opts[:id] ||= autogenerated_item_id
classes = [html_opts[:class], selected_class, active_leaf_class]
classes = classes.flatten.compact.join(' ')
html_opts[:class] = classes if classes && !classes.empty?
html_opts
end
# Returns the configured active_leaf_class if the item is the selected leaf,
# nil otherwise
def active_leaf_class
if !selected_by_subnav? && selected_by_condition?
config.active_leaf_class
end
end
# Returns the configured selected_class if the item is selected,
# nil otherwise
def selected_class
if selected?
container.selected_class || config.selected_class
end
end
# Returns the :highlights_on option as set at initialization
def highlights_on
@highlights_on ||= options[:highlights_on]
end
# Returns the :method option as set at initialization
def method
@method ||= options[:method]
end
# Returns the html attributes for the link as set with the :link_html option
# at initialization
def link_html_options
@link_html_options ||= options[:link_html]
end
protected
# Returns true if item has a subnavigation and
# the sub_navigation is selected
def selected_by_subnav?
sub_navigation && sub_navigation.selected?
end
# Returns true if the item's url matches the request's current url.
def selected_by_condition?
highlights_on ? selected_by_highlights_on? : selected_by_autohighlight?
end
# Returns true if both the item's url and the request's url are root_path
def root_path_match?
url == '/' && SimpleNavigation.request_path == '/'
end
# Returns the item's id which is added to the rendered output.
def autogenerated_item_id
config.id_generator.call(key) if config.autogenerate_item_ids
end
# Return true if auto_highlight is on for this item.
def auto_highlight?
config.auto_highlight && container.auto_highlight
end
private
attr_accessor :container,
:options
attr_writer :key,
:name,
:sub_navigation,
:url
def config
SimpleNavigation.config
end
def request_uri
SimpleNavigation.request_uri
end
def remove_anchors(url_with_anchors)
url_with_anchors && url_with_anchors.split('#').first
end
def remove_query_params(url_with_params)
url_with_params && url_with_params.split('?').first
end
def url_for_autohighlight
relevant_url = remove_anchors(self.url) if config.ignore_anchors_on_auto_highlight
relevant_url = remove_query_params(relevant_url) if config.ignore_query_params_on_auto_highlight
relevant_url
end
def selected_by_autohighlight?
return false unless auto_highlight?
root_path_match? ||
(url_for_autohighlight && SimpleNavigation.current_page?(url_for_autohighlight)) ||
autohighlight_by_subpath?
end
def autohighlight_by_subpath?
config.highlight_on_subpath && selected_by_subpath?
end
def selected_by_highlights_on?
case highlights_on
when Regexp then !!(request_uri =~ highlights_on)
when Proc then highlights_on.call
when :subpath then selected_by_subpath?
else
fail ArgumentError, ':highlights_on must be a Regexp, Proc or :subpath'
end
end
def selected_by_subpath?
escaped_url = Regexp.escape(url_for_autohighlight)
!!(request_uri =~ /^#{escaped_url}(\/|$||\?)/i)
end
def setup_sub_navigation(items = nil, &sub_nav_block)
return unless sub_nav_block || items
self.sub_navigation = ItemContainer.new(container.level + 1)
if sub_nav_block
sub_nav_block.call sub_navigation
else
sub_navigation.items = items
end
end
end
end
|