Welcome to mirror list, hosted at ThFree Co, Russian Federation.

navigation.rb « gitlab « lib - gitlab.com/gitlab-org/gitlab-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 25281f9c85937d0b2ce6170af3fc699e157be67b (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
require_relative '../helpers/generic'
require_relative '../helpers/icons_helper'

module Gitlab
  class Navigation
    include Nanoc::Helpers::Generic
    include Nanoc::Helpers::IconsHelper

    def initialize(items, item)
      @items = items
      @item = item

      disable_inactive_sections!
    end

    def nav_items
      @nav_items ||= nav_items_exists ? items[nav_items_dir] : items["/_data/default-nav.yaml"]
    end

    def element_href(element)
      is_ee_prefixed ? "/ee/#{element.url}" : "/#{dir}/#{element.url}"
    end

    def show_element?(element)
      item.path == "/#{dir}/#{element.url}"
    end

    def id_for(element)
      element.title.gsub(/[\s\/\(\)]/, '')
    end

    def optional_ee_badge(element)
      return unless element.ee_only?

      %(<span class="badges-drop global-nav-badges" data-toggle="tooltip" data-placement="top" title="Available in #{element.ee_tier}">#{icon('information-o', 14)}</span>)
    end

    def children
      @children ||= nav_items.fetch(:sections, []).map { |section| Section.new(section) }
    end

    private

    attr_reader :items, :item

    def disable_inactive_sections!
      return unless omnibus?

      children.each do |section|
        section.disable! unless has_active_element?([section])
      end
    end

    def has_active_element?(collection)
      return false unless collection

      collection.any? { |element| show_element?(element) || has_active_element?(element.children) }
    end

    def dir
      @dir ||= item.identifier.to_s[%r{(?<=/)[^/]+}]
    end

    def nav_items_dir
      @nav_items_dir ||= "/_data/#{dir}-nav.yaml"
    end

    def nav_items_exists
      !items[nav_items_dir].nil?
    end

    def is_ee_prefixed
      !nav_items_exists && dir != 'ce'
    end
  end
end