Sha256: 8fd9878ed6b9cf375f7c1cc01232c73ab3d4487e06073d1ad7c11b9a6873f13f

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module PandaCms
  class MenuComponent < ViewComponent::Base
    #
    # Renders the menu item and its children
    #
    # @param [String] name The name of the menu
    # @param [String] current_path The current path of the request (request.path)
    # @param [Hash] styles
    #  The CSS classes to apply to the menu items, containing "default", "inactive" and "active" keys.
    #  The "default" key is applied to all menu items. "inactive" and "active" are set based on the
    #  current path.
    # @return [void]
    def initialize(name:, current_path: "", styles: {})
      @menu = PandaCms::Menu.find_by(name: name)
      @menu_items = @menu.menu_items
      @menu_items = @menu_items.where("depth <= ?", @menu.depth) if @menu.depth
      @menu_items = @menu_items.order(:lft)
      @current_path = current_path.to_s

      @menu_items = @menu_items.order(:lft).map do |menu_item|
        if is_active?(menu_item)
          menu_item.define_singleton_method(:css_classes) { styles[:default] + " " + styles[:active] }
        else
          menu_item.define_singleton_method(:css_classes) { styles[:default] + " " + styles[:inactive] }
        end

        menu_item
      end
    end

    def is_active?(menu_item)
      return true if @current_path == "/" && active_link?(menu_item.page.path, match: :exact)
      return true if menu_item.page.path != "/" && active_link?(menu_item.page.path, match: :starts_with)
      false
    end

    def active_link?(path, match: :starts_with)
      if match == :starts_with
        return @current_path.starts_with?(path)
      elsif match == :exact
        return (@current_path == path)
      end

      false
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
panda_cms-0.6.0 app/components/panda_cms/menu_component.rb
panda_cms-0.5.10 app/components/panda_cms/menu_component.rb
panda_cms-0.5.9 app/components/panda_cms/menu_component.rb
panda_cms-0.5.8 app/components/panda_cms/menu_component.rb