diff options
author | Vasilii Iakliushin <viakliushin@gitlab.com> | 2020-11-06 13:35:18 +0300 |
---|---|---|
committer | Vasilii Iakliushin <viakliushin@gitlab.com> | 2020-11-06 17:59:40 +0300 |
commit | 3aa6e6e9e618cb68eb8557df744722b188f795bd (patch) | |
tree | 972c46927693901e397ec149c87fb8232b8a9c44 | |
parent | 211404eaaa607e2bb4cf0be980ce0aba360f9764 (diff) |
Extract Navigation class to manage layout variables
Contributes to https://gitlab.com/gitlab-org/gitlab-docs/-/issues/879
-rw-r--r-- | layouts/global_nav.html | 71 | ||||
-rw-r--r-- | lib/gitlab/navigation.rb | 58 | ||||
-rw-r--r-- | lib/gitlab/navigation/category.rb | 41 | ||||
-rw-r--r-- | lib/gitlab/navigation/doc.rb | 37 | ||||
-rw-r--r-- | lib/gitlab/navigation/section.rb | 37 |
5 files changed, 201 insertions, 43 deletions
diff --git a/layouts/global_nav.html b/layouts/global_nav.html index 334f4d85..1bf3795a 100644 --- a/layouts/global_nav.html +++ b/layouts/global_nav.html @@ -1,62 +1,47 @@ -<% -dir = @item.identifier.to_s[%r{(?<=/)[^/]+}] -nav_items_dir = "/_data/#{dir}-nav.yaml" -nav_items_exists = !@items[nav_items_dir].nil? -is_ee_prefixed = !nav_items_exists && dir != 'ce' -nav_items = nav_items_exists ? @items[nav_items_dir] : @items["/_data/default-nav.yaml"] -%> +<% navigation = Gitlab::Navigation.new(@items, @item) %> <nav class="global-nav-content"> <!-- nav sections --> - <% nav_items[:sections].each do |sec| %> + <% navigation.children.each do |sec| %> <div class="global-nav-section"> <span class="global-nav-block-top nav-link"> - <% section_href = is_ee_prefixed ? "/ee/#{sec[:section_url]}" : "/#{dir}/#{sec[:section_url]}" %> - <a class="global-nav-link level-0 <% if sec[:section_categories] %>has-collapse<% end %> <% if @item.path == "/#{dir}/#{sec[:section_url]}" %>active<% end %>" href="<%= section_href %>"> - <%= sec[:section_title] %> - <% if sec[:ee_only] %> - <span class="badges-drop global-nav-badges" data-toggle="tooltip" data-placement="top" title="Available in <%= sec[:ee_tier] %>"><i class="fa fa-info-circle" aria-hidden="true"></i></span> - <% end %><!-- end of if sec[:ee_only] --> + <a class="global-nav-link level-0 <%= sec.has_children? ? 'has-collapse' : '' %> <%= navigation.show_element?(sec) ? 'active' : '' %>" href="<%= navigation.element_href(sec) %>"> + <%= sec.title %> + <%= navigation.optional_ee_badge(sec) %> </a> - <div class="section-title <% if sec[:section_categories] %>collapse-toggle<% end %> <% if @item.path == "/#{dir}/#{sec[:section_url]}" %>active<% else %>collapsed<% end %>" data-toggle="collapse" aria-expanded="false" data-target="#<%= sec[:section_title].gsub(/[\s\/\(\)]/, '') %>"></div> + <div class="section-title <%= sec.has_children? ? 'collapse-toggle': '' %> <%= navigation.show_element?(sec) ? 'active' : 'collapsed' %>" data-toggle="collapse" aria-expanded="false" data-target="#<%= navigation.id_for(sec) %>"></div> </span> <!-- nav categories --> - <% if sec[:section_categories] %> - <div class="collapse <% if @item.path == "/#{dir}/#{sec[:section_url]}" %>show<% end %>" id="<%= sec[:section_title].gsub(/[\s\/\(\)]/, '') %>"> - <% sec[:section_categories].each do |cat| %> - <span class="global-nav-cat nav-link"> - <% if cat[:external_url] %> - <a class="global-nav-link level-1 <% if cat[:docs] %>has-collapse<% end %>" href="<%= cat[:category_url] %>" target="_blank"> - <%= cat[:category_title] %> - </a> - <% else %> - <% category_href = is_ee_prefixed ? "/ee/#{cat[:category_url]}" : "/#{dir}/#{cat[:category_url]}" %> - <a class="global-nav-link level-1 <% if cat[:docs] %>has-collapse<% end %> <% if @item.path == "/#{dir}/#{cat[:category_url]}" %>active<% end %>" href="<%= category_href %>"> - <%= cat[:category_title] %> - <% if cat[:ee_only] %> - <span class="badges-drop global-nav-badges" data-toggle="tooltip" data-placement="top" title="Available in <%= cat[:ee_tier] %>"><i class="fa fa-info-circle" aria-hidden="true"></i></span> - <% end %><!-- end of if cat[:ee_only] --> + <% if sec.has_children? %> + <div class="collapse <%= navigation.show_element?(sec) ? 'show' : '' %>" id="<%= navigation.id_for(sec) %>"> + <% sec.children.each do |cat| %> + <span class="global-nav-cat nav-link"> + <% if cat.external_url %> + <a class="global-nav-link level-1 <%= cat.has_children? ? 'has-collapse' : '' %>" href="<%= cat.url %>" target="_blank"> + <%= cat.title %> + </a> + <% else %> + <a class="global-nav-link level-1 <%= cat.has_children? ? 'has-collapse' : '' %> <%= navigation.show_element?(cat) ? 'active' : '' %>" href="<%= navigation.element_href(cat) %>"> + <%= cat.title %> + <%= navigation.optional_ee_badge(cat) %> </a> <% end %><!-- end of if cat[:external_url] --> - <div class="<% if cat[:docs] %>collapse-toggle<% end %> <% if @item.path == "/#{dir}/#{cat[:category_url]}" %>active<% else %>collapsed<% end %>" data-toggle="collapse" aria-expanded="false" data-target="#<%= cat[:category_title].gsub(/[\s\/\(\)]/, '') %>"></div> + <div class="<%= cat.has_children? ? 'collapse-toggle' : '' %> <%= navigation.show_element?(cat) ? 'active' : 'collapsed' %>" data-toggle="collapse" aria-expanded="false" data-target="#<%= navigation.id_for(cat) %>"></div> </span> <!-- nav docs --> - <% if cat[:docs] %> - <div class="collapse <% if @item.path == "/#{dir}/#{cat[:category_url]}" %>show<% end %>" id="<%= cat[:category_title].gsub(/[\s\/\(\)]/, '') %>"> - <% cat[:docs].each do |doc| %> + <% if cat.has_children? %> + <div class="collapse <%= navigation.show_element?(cat) ? 'show' : '' %>" id="<%= navigation.id_for(cat) %>"> + <% cat.children.each do |doc| %> <span class="nav-link"> - <% if doc[:external_url] %> - <a class="global-nav-link level-2" href="<%= doc[:doc_url] %>" target="_blank"> - <%= doc[:doc_title] %> + <% if doc.external_url %> + <a class="global-nav-link level-2" href="<%= doc.url %>" target="_blank"> + <%= doc.title %> </a> <% else%> - <% docs_href = is_ee_prefixed ? "/ee/#{doc[:doc_url]}" : "/#{dir}/#{doc[:doc_url]}" %> - <a class="global-nav-link level-2 <% if @item.path == "/#{dir}/#{doc[:doc_url]}" %>active<% end %>" href="<%= docs_href %>"> - <%= doc[:doc_title] %> - <% if doc[:ee_only] %> - <span class="badges-drop global-nav-badges" data-toggle="tooltip" data-placement="top" title="Available in <%= doc[:ee_tier] %>"><i class="fa fa-info-circle" aria-hidden="true"></i></span> - <% end %><!-- end if doc[:ee_only] --> + <a class="global-nav-link level-2 <%= navigation.show_element?(doc) ? 'active' : '' %>" href="<%= navigation.element_href(doc) %>"> + <%= doc.title %> + <%= navigation.optional_ee_badge(doc) %> </a> <% end %><!-- end of if doc[:external_url] --> </span> diff --git a/lib/gitlab/navigation.rb b/lib/gitlab/navigation.rb new file mode 100644 index 00000000..022654e0 --- /dev/null +++ b/lib/gitlab/navigation.rb @@ -0,0 +1,58 @@ +module Gitlab + class Navigation + def initialize(items, item) + @items = items + @item = item + 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}"><i class="fa fa-info-circle" aria-hidden="true"></i></span>] + end + + def children + @children ||= nav_items.fetch(:sections, []).map { |section| Section.new(section) } + end + + private + + attr_reader :items, :item + + 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 + + def is_omnibus? + ENV['NANOC_ENV'] == 'omnibus' + end + end +end diff --git a/lib/gitlab/navigation/category.rb b/lib/gitlab/navigation/category.rb new file mode 100644 index 00000000..75f401fa --- /dev/null +++ b/lib/gitlab/navigation/category.rb @@ -0,0 +1,41 @@ +module Gitlab + class Navigation + class Category + def initialize(category) + @category = category + end + + def title + category[:category_title] + end + + def external_url + category[:external_url] + end + + def url + category[:category_url] + end + + def ee_only? + category[:ee_only] + end + + def ee_tier + category[:ee_tier] + end + + def has_children? + !children.empty? + end + + def children + @children ||= category.fetch(:docs, []).map { |doc| Doc.new(doc) } + end + + private + + attr_reader :category + end + end +end diff --git a/lib/gitlab/navigation/doc.rb b/lib/gitlab/navigation/doc.rb new file mode 100644 index 00000000..8bed2d43 --- /dev/null +++ b/lib/gitlab/navigation/doc.rb @@ -0,0 +1,37 @@ +module Gitlab + class Navigation + class Doc + def initialize(doc) + @doc = doc + end + + def title + doc[:doc_title] + end + + def external_url + doc[:external_url] + end + + def url + doc[:doc_url] + end + + def ee_only? + doc[:ee_only] + end + + def ee_tier + doc[:ee_tier] + end + + def children + [] + end + + private + + attr_reader :doc + end + end +end diff --git a/lib/gitlab/navigation/section.rb b/lib/gitlab/navigation/section.rb new file mode 100644 index 00000000..8b761fb8 --- /dev/null +++ b/lib/gitlab/navigation/section.rb @@ -0,0 +1,37 @@ +module Gitlab + class Navigation + class Section + def initialize(section) + @section = section + end + + def title + section[:section_title] + end + + def ee_only? + section[:ee_only] + end + + def ee_tier + section[:ee_tier] + end + + def url + section[:section_url] + end + + def has_children? + !children.empty? + end + + def children + @children ||= section.fetch(:section_categories, []).map { |cat| Category.new(cat) } + end + + private + + attr_reader :section + end + end +end |