ichiban 1.2.3 → 1.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5c0d4eacb550103608173411c2d58f3efaa9f5e
4
- data.tar.gz: 7aeecd5c26d6eb6066e983f9f6237ac1b8fcab07
3
+ metadata.gz: e75247671503c6905c93fb55cca27be646d68626
4
+ data.tar.gz: 894a82e6eb0d165d5e6fa58b0517998f0d109a80
5
5
  SHA512:
6
- metadata.gz: c9bb87f05b4156797b0d36df4033e1003d0868a94bab4c25ff82436a0e7c9e74e59e3d85b06af3f9f4cb88598a3098979907fd96fad7e2d41471a61026ff9232
7
- data.tar.gz: db286ccf910880907d63961d40a93636c6f3b15b1c3d800ed93b206da66525e87165be758c55f0a4dd45c9035263b9b1a944cc0b77596935fa059a63fa5ca042
6
+ metadata.gz: 84d001d474a73ae9cdced63b8f3ecc801dd67f1d65dc866372b7bb15efc82df41371c4848e89e2d86879c8e4b5273f75299166b5f6f9c2a9f741ba6a909ecb7c
7
+ data.tar.gz: 0057724132944893a1b892193e84b36696c332762396059c441ac882e24c4992d9e627d0bc6775baa78f3d68d74bafbf908154792d5cd75b8e61244b8a37d741
@@ -5,6 +5,7 @@ module Ichiban
5
5
  unless items.is_a?(Array)
6
6
  raise "Expected first parameter of NavHelper#nav to be an Array, but got: #{items.inspect}"
7
7
  end
8
+ options = {sub_paths: :expand}.merge(options)
8
9
  Nav.new(items, options, self).to_html
9
10
  end
10
11
 
@@ -16,7 +17,7 @@ module Ichiban
16
17
  end
17
18
 
18
19
  def to_html
19
- ul(@items, 0)
20
+ ul(@items, 0, @options)
20
21
  end
21
22
 
22
23
  private
@@ -32,26 +33,28 @@ module Ichiban
32
33
  @ctx.path_with_slashes(@ctx.current_path).start_with?(@ctx.path_with_slashes(path))
33
34
  end
34
35
 
35
- # Recursive. Checks whether any item in the menu, or any item in any of its descendant
36
- # menus, has a path that *starts with* the current path. E.g. if a menu or its descendants
37
- # have a link to '/a/b/c/', and we're at '/a/b/c/d/e/f/', then this method returns true.
38
- def menu_matches_current_path?(items)
39
- !items.detect do |item|
40
- if current_path_starts_with?(item[1])
41
- # The current path matches this item, so we can stop looking.
42
- # menu_matches_current_path? will return true.
43
- true
44
- elsif item[2].is_a?(Array)
45
- # If an item has a sub-menu, then that menu must be the third element of the array.
46
- # (The format is [text, path, sub_menu, li_options].) So we recursively search the
47
- # descendant menu(s) of this item.
48
- menu_matches_current_path?(item[2])
49
- end
36
+ # Recursive.
37
+ def menu_matches_current_path?(path, sub_menu, options)
38
+ # If the menu item matches the current path exactly, return true.
39
+ current_path?(path) or
40
+
41
+ # If we're allowed to consider sub-paths, and the current path starts with the
42
+ # menu item's path, return true. E.g. current path /a/b/c/ and menu item path
43
+ # /a/b/.
44
+ (options[:sub_paths] != :collapse and current_path_starts_with?(path)) or
45
+
46
+ # The path for this menu item did not match. So search recursively for a matching
47
+ # path in this item's sub-menu.
48
+ !sub_menu.detect do |item|
49
+ # If an item has a sub-menu, then that menu must be the third element of the array.
50
+ # (The format is [text, path, sub_menu, li_options].) So we recursively search the
51
+ # descendant menu(s) of this item.
52
+ (item[2].is_a?(Array) and menu_matches_current_path?(item[1], item[2], options))
50
53
  end.nil?
51
54
  end
52
55
 
53
56
  # Recursive
54
- def ul(items, depth)
57
+ def ul(items, depth, options)
55
58
  # If we're in the outermost menu, add any passed-in <ul> attributes
56
59
  ul_options = (
57
60
  depth == 0 ?
@@ -97,36 +100,27 @@ module Ichiban
97
100
  lis << @ctx.content_tag('li', li_attrs) do
98
101
  li_inner_html = ''
99
102
 
103
+ sub_menu_open = (
104
+ !sub_menu.nil? and
105
+ menu_matches_current_path?(path, sub_menu, options)
106
+ )
107
+
100
108
  # Create the <a> or <span> tag for this item.
101
109
  if current_path?(path)
102
110
  li_inner_html << @ctx.content_tag('span', text, 'class' => 'selected')
103
111
  else
104
- if current_path_starts_with?(path)
112
+ if sub_menu_open
105
113
  a_attrs = {'class' => 'above-selected'}
106
114
  else
107
115
  a_attrs = {}
108
116
  end
109
117
  li_inner_html << @ctx.link_to(text, path, a_attrs)
110
118
  end
111
-
112
- # This item's sub-menu should be open if and only if:
113
- #
114
- # 1. we are at or inside the item's path; or
115
- # 2. we are at or inside a path included in any of this item's descendant menus.
116
-
117
- if sub_menu
118
- sub_menu_open = (
119
- current_path_starts_with?(path) or
120
- menu_matches_current_path?(sub_menu)
121
- )
122
- else
123
- sub_menu_open = false
124
- end
125
119
 
126
120
  # If the sub-menu is open, then we recursively generate its HTML
127
121
  # and append it to this <li>.
128
122
  if sub_menu_open
129
- li_inner_html << ul(sub_menu, depth + 1)
123
+ li_inner_html << ul(sub_menu, depth + 1, options)
130
124
  end
131
125
 
132
126
  li_inner_html
@@ -1,3 +1,3 @@
1
1
  module Ichiban
2
- VERSION = '1.2.3'
2
+ VERSION = '1.2.6'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ichiban
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jarrett Colby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-27 00:00:00.000000000 Z
11
+ date: 2015-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ejs