class SwedbankPay::SidebarPage

Represents a jekyll_page in the Sidebar

Constants

FIXNUM_MAX

Attributes

children[R]
doc[R]
filename[R]
headers[RW]
level[R]
name[R]
number[RW]
order[R]
parent[RW]
path[R]
sidebar_container[RW]
title[R]

Public Class Methods

new(jekyll_page) click to toggle source
# File lib/sidebar_page.rb, line 19
def initialize(jekyll_page)
  raise ArgumentError, 'jekyll_page cannot be nil' if jekyll_page.nil?
  raise ArgumentError, 'jekyll_page must be a Jekyll::Page' unless jekyll_page.is_a? Jekyll::Page

  @filename = jekyll_page.destination('')
  @jekyll_page = jekyll_page
  sidebar_path = SidebarPath.new(jekyll_page['url'])
  @path = sidebar_path.to_s
  @parent = sidebar_path.parent
  @level = sidebar_path.level
  @name = sidebar_path.name
  @hide_from_sidebar = jekyll_page['hide_from_sidebar'].nil? ? false : jekyll_page['hide_from_sidebar']
  @title = SidebarPageTitle.parse(jekyll_page, self)
  @order = menu_order(jekyll_page)
  @children = SidebarPageCollection.new(self)
  @card_overview = jekyll_page['card_overview'].nil? ? false : jekyll_page['card_overview']
  @anchor_headings = jekyll_page['anchor_headings'].nil? ? true : jekyll_page['anchor_headings']
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/sidebar_page.rb, line 95
def <=>(other)
  return -1 if other.nil?

  if @order == FIXNUM_MAX && other.order == FIXNUM_MAX
    return 0 if @title.nil? && other.title.nil?
    return -1 if other.title.nil?
    return 1 if title.nil?

    return @title <=> other.title
  end

  @order <=> other.order
end
active?(current, is_leaf: false) click to toggle source
# File lib/sidebar_page.rb, line 38
def active?(current, is_leaf: false)
  current_path = find_path(current)

  return true if @path == current_path

  # If we're on a leaf node item, such as when rendering the first header
  # item of a sub-group, its children's active state must be disregarded.
  unless is_leaf
    @children.each do |child|
      return true if child.active?(current_path, is_leaf: is_leaf)
    end
  end

  false
end
anchor_headings?() click to toggle source
# File lib/sidebar_page.rb, line 160
def anchor_headings?
  @anchor_headings
end
card_overview?() click to toggle source
# File lib/sidebar_page.rb, line 156
def card_overview?
  @card_overview
end
children=(children) click to toggle source
# File lib/sidebar_page.rb, line 83
def children=(children)
  @children = SidebarPageCollection.new(self, children)
end
children?() click to toggle source
# File lib/sidebar_page.rb, line 132
def children?
  !children.nil? && children.any?
end
coordinate() click to toggle source
# File lib/sidebar_page.rb, line 140
def coordinate
  return @number.to_s if @parent.nil?
  return @number.to_s unless @parent.respond_to? :coordinate

  "#{@parent.coordinate}.#{@number}"
end
enrich_jekyll() click to toggle source
# File lib/sidebar_page.rb, line 109
def enrich_jekyll
  if @title.nil?
    SidebarLogger.debug("No title for #{@name}")
    return
  end

  SidebarLogger.debug("<#{@path}>.lead_title('#{@title.lead}').main_title('#{@title.main}')")

  @jekyll_page.data['lead_title'] = @title.lead
  @jekyll_page.data['main_title'] = @title.main
  @jekyll_page.data['children'] = @children
  @jekyll_page.data['absolute_path'] = @path
end
headers?() click to toggle source
# File lib/sidebar_page.rb, line 136
def headers?
  !headers.nil? && headers.any?
end
hidden?() click to toggle source
# File lib/sidebar_page.rb, line 54
def hidden?
  return true if @title.nil?
  return true if @hide_from_sidebar

  false
end
hidden_for?(other_page) click to toggle source
# File lib/sidebar_page.rb, line 61
def hidden_for?(other_page)
  if @jekyll_page.data['layout'] == 'search'
    # Never show the search result page in the menu.
    return true
  end

  # The current page should be hidden for the other page unless the
  # other page is also hidden.
  hidden = hidden?

  if other_page.nil? || !other_page.is_a?(SidebarPage)
    SidebarLogger.debug("Other page '#{other_page}' is nil or not a SidebarPage")
    return hidden
  end

  # If the other page is hidden, the current page should not be hidden
  # from it.
  return false if other_page.hidden? && in_same_section_as?(other_page)

  hidden
end
inspect() click to toggle source
# File lib/sidebar_page.rb, line 91
def inspect
  to_s
end
load() click to toggle source
# File lib/sidebar_page.rb, line 147
def load
  @doc = File.open(@filename) { |f| Nokogiri::HTML(f) }
  @doc
end
save() click to toggle source
# File lib/sidebar_page.rb, line 123
def save
  SidebarLogger.debug("   Writing Sidebar: #{filename}")

  File.open(@filename, 'w') do |file|
    html = @doc.to_html(encoding: 'UTF-8')
    file.write(html)
  end
end
to_liquid() click to toggle source
# File lib/sidebar_page.rb, line 152
def to_liquid
  @jekyll_page.to_liquid
end
to_s() click to toggle source
# File lib/sidebar_page.rb, line 87
def to_s
  SidebarTextBuilder.new(self).to_s
end

Private Instance Methods

child_of?(path) click to toggle source
# File lib/sidebar_page.rb, line 177
def child_of?(path)
  @path.split('/').length > @level && path.start_with?(@path)
end
eq?(path) click to toggle source
# File lib/sidebar_page.rb, line 173
def eq?(path)
  @path == path
end
find_path(current) click to toggle source
# File lib/sidebar_page.rb, line 181
def find_path(current)
  if current.nil?
    SidebarLogger.warn('Nil current_page')
    return ''
  end

  return current if current.is_a? String
  return current.path if current.respond_to?(:path)

  SidebarLogger.warn("#{current.class} ('#{current}') does not respond to :path.")

  ''
end
in_same_section_as?(other_page) click to toggle source
# File lib/sidebar_page.rb, line 195
def in_same_section_as?(other_page)
  # If this or the other page is the root index page, just ignore the
  # hidden state completely
  return false if other_page.path == '/' || @path == '/'

  other_page.path.start_with?(@path) || @path.start_with?(other_page.path)
end
menu_order(jekyll_page) click to toggle source