Class: Playbook::Pagination::Rails
- Inherits:
-
WillPaginate::ActionView::LinkRenderer
- Object
- WillPaginate::ActionView::LinkRenderer
- Playbook::Pagination::Rails
- Defined in:
- lib/playbook/pagination_renderer.rb
Instance Attribute Summary collapse
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
- #container_attributes ⇒ Object
- #desktop_items ⇒ Object
- #gap ⇒ Object
- #link(text, target, attributes = {}) ⇒ Object
- #mobile_dropdown ⇒ Object
- #mobile_next_button ⇒ Object
- #mobile_pagination ⇒ Object
- #mobile_prev_button ⇒ Object
- #next_page ⇒ Object
- #page_number(page) ⇒ Object
- #prepare(collection, options, template) ⇒ Object
- #previous_or_next_page(page, text, classname) ⇒ Object
- #previous_page ⇒ Object
- #to_html ⇒ Object
Instance Attribute Details
#template ⇒ Object (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_attributes ⇒ Object
10 11 12 |
# File 'lib/playbook/pagination_renderer.rb', line 10 def container_attributes { class: "pb_paginate" } end |
#desktop_items ⇒ Object
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 |
#gap ⇒ Object
70 71 72 |
# File 'lib/playbook/pagination_renderer.rb', line 70 def gap template.raw("") end |
#link(text, target, attributes = {}) ⇒ Object
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_dropdown ⇒ Object
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.content_tag(:span, current_page.to_s, class: "pagination-dropdown-current"), template.content_tag(:span, "of #{@collection.total_pages}", class: "pagination-dropdown-total"), template.content_tag(:span, chevron_down_svg, class: "pagination-dropdown-icon"), ] trigger_content = template.safe_join(trigger_parts, " ") trigger = template.content_tag(: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.content_tag(:div, link("Page #{page}", page), class: option_class) end = template.content_tag(:div, template.safe_join(option_nodes), class: "pagination-dropdown-menu below") template.content_tag(:details, template.safe_join([trigger, ]), class: "pagination-dropdown") end |
#mobile_next_button ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/playbook/pagination_renderer.rb', line 97 def page = @collection.current_page < @collection.total_pages && @collection.current_page + 1 classes = "pagination-right#{page ? '' : ' disabled'}" label = template.content_tag(:span, "Next", class: "pagination-mobile-label") text = template.safe_join([label, chevron_right_svg]) if page template.content_tag(:li, link(text, page), class: classes) else template.content_tag(:li, template.content_tag(:span, text), class: classes) end end |
#mobile_pagination ⇒ Object
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_dropdown, , ].compact) template.content_tag(:div, content, class: "pb_pagination_mobile pagination-mobile rails-pagination-mobile") end |
#mobile_prev_button ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/playbook/pagination_renderer.rb', line 84 def page = @collection.current_page > 1 && @collection.current_page - 1 classes = "pagination-left#{page ? '' : ' disabled'}" label = template.content_tag(:span, "Prev", class: "pagination-mobile-label") text = template.safe_join([chevron_left_svg, label]) if page template.content_tag(:li, link(text, page), class: classes) else template.content_tag(:li, template.content_tag(:span, text), class: classes) end end |
#next_page ⇒ Object
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.content_tag(:li, template.content_tag(:span, page), class: classes) else template.content_tag(: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, , 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.content_tag(:li, link(text, page), class: classname) else template.content_tag(:li, template.content_tag(:span, text), class: "#{classname} disabled") end end |
#previous_page ⇒ Object
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_html ⇒ Object
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.content_tag( :div, template.safe_join(desktop_items, @options[:link_separator].to_s), class: "pagination-desktop" ) mobile = mobile_pagination inner = template.content_tag(:div, template.safe_join([desktop, mobile]), class: "pb_pagination rails-pagination-mobile") template.content_tag(:div, inner, **container_attributes) end |