summaryrefslogtreecommitdiff
path: root/app/helpers/navigation_helper.rb
blob: 8582391ccd67d51fd4f938004d9a05ae90a533e2 (plain)
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
module NavigationHelper

  def has_side_column?
    second_level_children_count = Menu.menu.submenu(current_page_path.first).try(:size)
    if second_level_children_count.nil?
      false
    else
      second_level_children_count > 1
    end
  end

  def top_level_navigation_links
    haml do
      first = 'first'
      Menu.menu.each do |item|
        active = current_page_path.first == item.name ? 'active' : ''
        haml 'li.tab', :class => first do
          haml 'a.tab', I18n.t('pages.' + item.name), :href => menu_item_path(item), :class => active
        end
        first = ''
      end
    end
  end

  def side_column_navigation_links
    if menu = Menu.menu.submenu(current_page_path.first)
      haml do
        haml 'ul.nav.nav-tabs.nav-stacked' do
          display_menu(menu, 1)
        end
      end
    end
  end

  def act_as(page)
    page = StaticPage.find(page)
    @current_page_path = page.path
    render_page(page)
  end

  private

  def menu_item_path(item)
    "/#{I18n.locale}/#{item.path.join('/')}"
  end

  def display_menu(menu, level=0)
    menu.each do |item|
      haml 'li', :class => path_active(current_page_path, item) do
        haml 'a', I18n.t('pages.'+item.name), :href => menu_item_path(item), :class => "level#{level}"
      end
      display_menu(item.submenu, level+1)
    end
  end

  def path_active(page_path, menu_item)
    array_starts_with(page_path, menu_item.path) ? 'active' : ''
  end

  def current_page_path
    @current_page_path ||= begin
      if @page
        @page.path
      elsif params[:page].is_a? String
        params[:page].split('/')
      else
        []
      end
    end
  end

  #
  # returns true if first part of array_long contains array_short
  #
  def array_starts_with(array_long, array_short)
    array_short.length.times do |i|
      if array_short[i] != array_long[i]
        return false
      end
    end
    return true
  end

end