Class: Playbook::Pagination::Rails

Inherits:
WillPaginate::ActionView::LinkRenderer
  • Object
show all
Defined in:
lib/playbook/pagination_renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#templateObject (readonly)

Returns the value of attribute template.



8
9
10
# File 'lib/playbook/pagination_renderer.rb', line 8

def template
  @template
end

Instance Method Details

#container_attributesObject



10
11
12
# File 'lib/playbook/pagination_renderer.rb', line 10

def container_attributes
  { class: "pb_paginate" }
end

#desktop_itemsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/playbook/pagination_renderer.rb', line 37

def desktop_items
  items = []
  items << previous_page

  items.concat(
    windowed_page_numbers.map do |page|
      page == :gap ? gap : page_number(page)
    end
  )

  items << next_page
  items.compact
end

#gapObject



70
71
72
# File 'lib/playbook/pagination_renderer.rb', line 70

def gap
  template.raw("")
end


19
20
21
# File 'lib/playbook/pagination_renderer.rb', line 19

def link(text, target, attributes = {})
  template.link_to(template.raw(text.to_s), url(target), attributes)
end

#mobile_dropdownObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/playbook/pagination_renderer.rb', line 110

def mobile_dropdown
  trigger_parts = [
    template.(:span, current_page.to_s, class: "pagination-dropdown-current"),
    template.(:span, "of #{@collection.total_pages}", class: "pagination-dropdown-total"),
    template.(:span, chevron_down_svg, class: "pagination-dropdown-icon"),
  ]
  trigger_content = template.safe_join(trigger_parts, " ")

  trigger = template.(:summary, trigger_content, class: "pagination-dropdown-trigger rails-trigger", role: "button")

  option_nodes = (1..@collection.total_pages).map do |page|
    option_class = "pagination-dropdown-option"
    option_class += " active" if page == current_page
    template.(:div, link("Page #{page}", page), class: option_class)
  end

  menu = template.(:div, template.safe_join(option_nodes), class: "pagination-dropdown-menu below")
  template.(:details, template.safe_join([trigger, menu]), class: "pagination-dropdown")
end

#mobile_next_buttonObject



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

def mobile_next_button
  page = @collection.current_page < @collection.total_pages && @collection.current_page + 1
  classes = "pagination-right#{page ? '' : ' disabled'}"
  label = template.(:span, "Next", class: "pagination-mobile-label")
  text = template.safe_join([label, chevron_right_svg])

  if page
    template.(:li, link(text, page), class: classes)
  else
    template.(:li, template.(:span, text), class: classes)
  end
end

#mobile_paginationObject



74
75
76
77
78
79
80
81
82
# File 'lib/playbook/pagination_renderer.rb', line 74

def mobile_pagination
  content = template.safe_join([
    mobile_prev_button,
    mobile_dropdown,
    mobile_next_button,
  ].compact)

  template.(:div, content, class: "pb_pagination_mobile pagination-mobile rails-pagination-mobile")
end

#mobile_prev_buttonObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/playbook/pagination_renderer.rb', line 84

def mobile_prev_button
  page = @collection.current_page > 1 && @collection.current_page - 1
  classes = "pagination-left#{page ? '' : ' disabled'}"
  label = template.(:span, "Prev", class: "pagination-mobile-label")
  text = template.safe_join([chevron_left_svg, label])

  if page
    template.(:li, link(text, page), class: classes)
  else
    template.(:li, template.(:span, text), class: classes)
  end
end

#next_pageObject



139
140
141
142
143
144
145
146
# File 'lib/playbook/pagination_renderer.rb', line 139

def next_page
  num = @collection.current_page < @collection.total_pages && @collection.current_page + 1
  previous_or_next_page(
    num,
    chevron_right_svg,
    "pagination-right"
  )
end

#page_number(page) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/playbook/pagination_renderer.rb', line 51

def page_number(page)
  classes = "pagination-number"
  classes += " active" if page == current_page

  if page == current_page
    template.(:li, template.(:span, page), class: classes)
  else
    template.(:li, link(page, page, rel: rel_value(page)), class: classes)
  end
end

#prepare(collection, options, template) ⇒ Object



14
15
16
17
# File 'lib/playbook/pagination_renderer.rb', line 14

def prepare(collection, options, template)
  super
  @template = template
end

#previous_or_next_page(page, text, classname) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/playbook/pagination_renderer.rb', line 62

def previous_or_next_page(page, text, classname)
  if page
    template.(:li, link(text, page), class: classname)
  else
    template.(:li, template.(:span, text), class: "#{classname} disabled")
  end
end

#previous_pageObject



130
131
132
133
134
135
136
137
# File 'lib/playbook/pagination_renderer.rb', line 130

def previous_page
  num = @collection.current_page > 1 && @collection.current_page - 1
  previous_or_next_page(
    num,
    chevron_left_svg,
    "pagination-left"
  )
end

#to_htmlObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/playbook/pagination_renderer.rb', line 23

def to_html
  return "" if @collection.total_pages <= 1

  desktop = template.(
    :div,
    template.safe_join(desktop_items, @options[:link_separator].to_s),
    class: "pagination-desktop"
  )
  mobile = mobile_pagination

  inner = template.(:div, template.safe_join([desktop, mobile]), class: "pb_pagination rails-pagination-mobile")
  template.(:div, inner, **container_attributes)
end