added capacity for pulling static pages from multiple directory source trees.
[leap_website.git] / app / helpers / navigation_helper.rb
1 module NavigationHelper
2
3   def has_side_column?
4     if root_page?
5       return false
6     end
7     second_level_children_count = site.menu.submenu(current_page_path.first).try(:size)
8     if second_level_children_count.nil?
9       false
10     else
11       second_level_children_count >= 1
12     end
13   end
14
15   def top_level_navigation_links
16     haml do
17       first = 'first'
18       site.menu.each do |item|
19         active = current_page_path.first == item.name ? 'active' : ''
20         haml 'li.tab', :class => first do
21           haml 'a.tab', I18n.t('pages.' + item.name), :href => menu_item_path(item), :class => active
22         end
23         first = ''
24       end
25     end
26   end
27
28   def side_column_navigation_links
29     if menu = site.menu.submenu(current_page_path.first)
30       haml do
31         haml 'ul.nav.nav-tabs.nav-stacked' do
32           display_menu(menu, 1)
33         end
34       end
35     end
36   end
37
38   def act_as(page)
39     page = site.find_page(page)
40     @current_page_path = page.path
41     render_page(page)
42   end
43
44   private
45
46   def menu_item_path(item)
47     "/#{I18n.locale}/#{item.path.join('/')}"
48   end
49
50   def display_menu(menu, level=0)
51     menu.each do |item|
52       haml 'li', :class => path_active(current_page_path, item) do
53         haml 'a', I18n.t('pages.'+item.name), :href => menu_item_path(item), :class => "level#{level}"
54       end
55       display_menu(item.submenu, level+1)
56     end
57   end
58
59   def path_active(page_path, menu_item)
60     active = ''
61     if menu_item.path == page_path
62       active = 'active'
63     elsif menu_item.path_prefix_of?(page_path)
64       if menu_item.leaf_for_path?(page_path)
65         active = 'active'
66       else
67         active = 'semi-active'
68       end
69     end
70     active
71   end
72
73   def current_page_path
74     @current_page_path ||= begin
75       if @page
76         @page.path
77       elsif params[:page].is_a? String
78         params[:page].split('/')
79       else
80         []
81       end
82     end
83   end
84
85   #
86   # the usage of 'home' as the default root page is hardcoded right now in the routes.
87   # this should be changed in the future.
88   #
89   def root_page?
90     @page && @page.path == ['home']
91   end
92
93 end