Commit 585fab33 authored by Arturo Herrero's avatar Arturo Herrero

Merge branch 'fj-move-sidebar-code-to-lib' into 'master'

Move project sidebar refactor to 'lib' folder

See merge request gitlab-org/gitlab!59809
parents a63b410d d00be9a0
# frozen_string_literal: true
module Sidebars
module ContainerWithHtmlOptions
# The attributes returned from this method
# will be applied to helper methods like
# `link_to` or the div containing the container.
def container_html_options
{
aria: { label: title }
}.merge(extra_container_html_options)
end
# Classes will override mostly this method
# and not `container_html_options`.
def extra_container_html_options
{}
end
# Attributes to pass to the html_options attribute
# in the helper method that sets the active class
# on each element.
def nav_link_html_options
{}
end
def title
raise NotImplementedError
end
# The attributes returned from this method
# will be applied right next to the title,
# for example in the span that renders the title.
def title_html_options
{}
end
def link
raise NotImplementedError
end
end
end
# frozen_string_literal: true
module Sidebars
module HasActiveRoutes
# This method will indicate for which paths or
# controllers, the menu or menu item should
# be set as active.
#
# The returned values are passed to the `nav_link` helper method,
# so the params can be either `path`, `page`, `controller`.
# Param 'action' is not supported.
def active_routes
{}
end
end
end
# frozen_string_literal: true
# This module handles elements in a list. All elements
# must have a different class
module Sidebars
module PositionableList
def add_element(list, element)
list << element
end
def insert_element_before(list, before_element, new_element)
index = index_of(list, before_element)
if index
list.insert(index, new_element)
else
list.unshift(new_element)
end
end
def insert_element_after(list, after_element, new_element)
index = index_of(list, after_element)
if index
list.insert(index + 1, new_element)
else
add_element(list, new_element)
end
end
private
def index_of(list, element)
list.index { |e| e.is_a?(element) }
end
end
end
# frozen_string_literal: true
module Sidebars
module Renderable
# This method will control whether the menu or menu_item
# should be rendered. It will be overriden by specific
# classes.
def render?
true
end
end
end
# frozen_string_literal: true
module Sidebars
module Concerns
module ContainerWithHtmlOptions
# The attributes returned from this method
# will be applied to helper methods like
# `link_to` or the div containing the container.
def container_html_options
{
aria: { label: title }
}.merge(extra_container_html_options)
end
# Classes will override mostly this method
# and not `container_html_options`.
def extra_container_html_options
{}
end
# Attributes to pass to the html_options attribute
# in the helper method that sets the active class
# on each element.
def nav_link_html_options
{}
end
def title
raise NotImplementedError
end
# The attributes returned from this method
# will be applied right next to the title,
# for example in the span that renders the title.
def title_html_options
{}
end
def link
raise NotImplementedError
end
end
end
end
# frozen_string_literal: true
module Sidebars
module Concerns
module HasActiveRoutes
# This method will indicate for which paths or
# controllers, the menu or menu item should
# be set as active.
#
# The returned values are passed to the `nav_link` helper method,
# so the params can be either `path`, `page`, `controller`.
# Param 'action' is not supported.
def active_routes
{}
end
end
end
end
...@@ -4,13 +4,15 @@ ...@@ -4,13 +4,15 @@
# hints for menus. Hints are elements displayed # hints for menus. Hints are elements displayed
# when the user hover the menu item. # when the user hover the menu item.
module Sidebars module Sidebars
module HasHint module Concerns
def show_hint? module HasHint
false def show_hint?
end false
end
def hint_html_options def hint_html_options
{} {}
end
end end
end end
end end
...@@ -3,25 +3,27 @@ ...@@ -3,25 +3,27 @@
# This module has the necessary methods to show # This module has the necessary methods to show
# sprites or images next to the menu item. # sprites or images next to the menu item.
module Sidebars module Sidebars
module HasIcon module Concerns
def sprite_icon module HasIcon
nil def sprite_icon
end nil
end
def sprite_icon_html_options def sprite_icon_html_options
{} {}
end end
def image_path def image_path
nil nil
end end
def image_html_options def image_html_options
{} {}
end end
def icon_or_image? def icon_or_image?
sprite_icon || image_path sprite_icon || image_path
end
end end
end end
end end
...@@ -3,19 +3,21 @@ ...@@ -3,19 +3,21 @@
# This module introduces the logic to show the "pill" element # This module introduces the logic to show the "pill" element
# next to the menu item, indicating the a count. # next to the menu item, indicating the a count.
module Sidebars module Sidebars
module HasPill module Concerns
def has_pill? module HasPill
false def has_pill?
end false
end
# In this method we will need to provide the query # In this method we will need to provide the query
# to retrieve the elements count # to retrieve the elements count
def pill_count def pill_count
raise NotImplementedError raise NotImplementedError
end end
def pill_html_options def pill_html_options
{} {}
end
end end
end end
end end
# frozen_string_literal: true
# This module handles elements in a list. All elements
# must have a different class
module Sidebars
module Concerns
module PositionableList
def add_element(list, element)
list << element
end
def insert_element_before(list, before_element, new_element)
index = index_of(list, before_element)
if index
list.insert(index, new_element)
else
list.unshift(new_element)
end
end
def insert_element_after(list, after_element, new_element)
index = index_of(list, after_element)
if index
list.insert(index + 1, new_element)
else
add_element(list, new_element)
end
end
private
def index_of(list, element)
list.index { |e| e.is_a?(element) }
end
end
end
end
# frozen_string_literal: true
module Sidebars
module Concerns
module Renderable
# This method will control whether the menu or menu_item
# should be rendered. It will be overriden by specific
# classes.
def render?
true
end
end
end
end
...@@ -6,12 +6,12 @@ module Sidebars ...@@ -6,12 +6,12 @@ module Sidebars
include ::Gitlab::Routing include ::Gitlab::Routing
include GitlabRoutingHelper include GitlabRoutingHelper
include Gitlab::Allowable include Gitlab::Allowable
include ::Sidebars::HasPill include ::Sidebars::Concerns::HasPill
include ::Sidebars::HasIcon include ::Sidebars::Concerns::HasIcon
include ::Sidebars::PositionableList include ::Sidebars::Concerns::PositionableList
include ::Sidebars::Renderable include ::Sidebars::Concerns::Renderable
include ::Sidebars::ContainerWithHtmlOptions include ::Sidebars::Concerns::ContainerWithHtmlOptions
include ::Sidebars::HasActiveRoutes include ::Sidebars::Concerns::HasActiveRoutes
attr_reader :context attr_reader :context
delegate :current_user, :container, to: :@context delegate :current_user, :container, to: :@context
......
...@@ -6,11 +6,11 @@ module Sidebars ...@@ -6,11 +6,11 @@ module Sidebars
include ::Gitlab::Routing include ::Gitlab::Routing
include GitlabRoutingHelper include GitlabRoutingHelper
include Gitlab::Allowable include Gitlab::Allowable
include ::Sidebars::HasIcon include ::Sidebars::Concerns::HasIcon
include ::Sidebars::HasHint include ::Sidebars::Concerns::HasHint
include ::Sidebars::Renderable include ::Sidebars::Concerns::Renderable
include ::Sidebars::ContainerWithHtmlOptions include ::Sidebars::Concerns::ContainerWithHtmlOptions
include ::Sidebars::HasActiveRoutes include ::Sidebars::Concerns::HasActiveRoutes
attr_reader :context attr_reader :context
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
module Sidebars module Sidebars
class Panel class Panel
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
include ::Sidebars::PositionableList include ::Sidebars::Concerns::PositionableList
attr_reader :context, :scope_menu, :hidden_menu attr_reader :context, :scope_menu, :hidden_menu
......
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Sidebars::ContainerWithHtmlOptions do RSpec.describe Sidebars::Concerns::ContainerWithHtmlOptions do
subject do subject do
Class.new do Class.new do
include Sidebars::ContainerWithHtmlOptions include Sidebars::Concerns::ContainerWithHtmlOptions
def title def title
'Foo' 'Foo'
......
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Sidebars::PositionableList do RSpec.describe Sidebars::Concerns::PositionableList do
subject do subject do
Class.new do Class.new do
include Sidebars::PositionableList include Sidebars::Concerns::PositionableList
end.new end.new
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment