Class: SwedbankPay::SidebarPage

Inherits:
Object
  • Object
show all
Defined in:
lib/sidebar_page.rb

Overview

Represents a jekyll_page in the Sidebar

Constant Summary collapse

FIXNUM_MAX =
((2**((0.size * 8) - 2)) - 1)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jekyll_page) ⇒ SidebarPage

Returns a new instance of SidebarPage.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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
  @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)
  @section = jekyll_page['section']
  @icon = jekyll_page['sidebar_icon']
  @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

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



16
17
18
# File 'lib/sidebar_page.rb', line 16

def children
  @children
end

#docObject (readonly)

Returns the value of attribute doc.



16
17
18
# File 'lib/sidebar_page.rb', line 16

def doc
  @doc
end

#filenameObject (readonly)

Returns the value of attribute filename.



16
17
18
# File 'lib/sidebar_page.rb', line 16

def filename
  @filename
end

#headersObject

Returns the value of attribute headers.



17
18
19
# File 'lib/sidebar_page.rb', line 17

def headers
  @headers
end

#iconObject (readonly)

Returns the value of attribute icon.



16
17
18
# File 'lib/sidebar_page.rb', line 16

def icon
  @icon
end

#levelObject (readonly)

Returns the value of attribute level.



16
17
18
# File 'lib/sidebar_page.rb', line 16

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/sidebar_page.rb', line 16

def name
  @name
end

#numberObject

Returns the value of attribute number.



17
18
19
# File 'lib/sidebar_page.rb', line 17

def number
  @number
end

#orderObject (readonly)

Returns the value of attribute order.



16
17
18
# File 'lib/sidebar_page.rb', line 16

def order
  @order
end

#parentObject

Returns the value of attribute parent.



17
18
19
# File 'lib/sidebar_page.rb', line 17

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



16
17
18
# File 'lib/sidebar_page.rb', line 16

def path
  @path
end

#sectionObject (readonly)

Returns the value of attribute section.



16
17
18
# File 'lib/sidebar_page.rb', line 16

def section
  @section
end

Returns the value of attribute sidebar_container.



17
18
19
# File 'lib/sidebar_page.rb', line 17

def sidebar_container
  @sidebar_container
end

#titleObject (readonly)

Returns the value of attribute title.



16
17
18
# File 'lib/sidebar_page.rb', line 16

def title
  @title
end

Instance Method Details

#<=>(other) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sidebar_page.rb', line 96

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) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sidebar_page.rb', line 39

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?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/sidebar_page.rb', line 161

def anchor_headings?
  @anchor_headings
end

#card_overview?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/sidebar_page.rb', line 157

def card_overview?
  @card_overview
end

#children?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/sidebar_page.rb', line 133

def children?
  !children.nil? && children.any?
end

#coordinateObject



141
142
143
144
145
146
# File 'lib/sidebar_page.rb', line 141

def coordinate
  return @number.to_s if @parent.nil?
  return @number.to_s unless @parent.respond_to? :coordinate

  "#{@parent.coordinate}.#{@number}"
end

#enrich_jekyllObject



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/sidebar_page.rb', line 110

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?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/sidebar_page.rb', line 137

def headers?
  !headers.nil? && headers.any?
end

#hidden?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'lib/sidebar_page.rb', line 55

def hidden?
  return true if @title.nil?
  return true if @hide_from_sidebar

  false
end

#hidden_for?(other_page) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sidebar_page.rb', line 62

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

#inspectObject



92
93
94
# File 'lib/sidebar_page.rb', line 92

def inspect
  to_s
end

#loadObject



148
149
150
151
# File 'lib/sidebar_page.rb', line 148

def load
  @doc = File.open(@filename) { |f| Nokogiri::HTML(f) }
  @doc
end

#saveObject



124
125
126
127
128
129
130
131
# File 'lib/sidebar_page.rb', line 124

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_liquidObject



153
154
155
# File 'lib/sidebar_page.rb', line 153

def to_liquid
  @jekyll_page.to_liquid
end

#to_sObject



88
89
90
# File 'lib/sidebar_page.rb', line 88

def to_s
  SidebarTextBuilder.new(self).to_s
end