1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| require 'cgi' require 'will_paginate/core_ext' require 'will_paginate/view_helpers' require 'will_paginate/view_helpers/link_renderer_base'
module WillPaginate module ViewHelpers class LinkRenderer < LinkRendererBase def prepare(collection, options, template) super(collection, options) @template = template @container_attributes = @base_url_params = nil end
def to_html html = pagination.map do |item| item.is_a?(Fixnum) ? page_number(item) : send(item) end.join(@options[:link_separator]) @options[:container] ? html_container(html) : html end
def container_attributes @container_attributes ||= @options.except(*(ViewHelpers.pagination_options.keys + [:renderer] - [:class])) end protected def page_number(page) unless page == current_page tag :li, link(page, page, :rel => rel_value(page)), :class => 'waves-effect' else tag(:li, link(page, '#!', :rel => rel_value(page)), :class => 'active') end end def gap text = @template.will_paginate_translate(:page_gap) { '…' } %(<span class="gap">#{text}</span>) end def previous_page num = @collection.current_page > 1 && @collection.current_page - 1 previous_or_next_page(num, 'chevron_left', 'previous_page') end def next_page num = @collection.current_page < total_pages && @collection.current_page + 1 previous_or_next_page(num, 'chevron_right', 'next_page') end def previous_or_next_page(page, text, classname) icon_item = tag :i, text, :class => 'material-icons' if page tag(:li, link(icon_item, page), :class => 'waves-effect') else tag(:li, link(icon_item, '#!'), :class => 'disabled') end end def html_container(html) tag(:div, html, container_attributes) end def url(page) raise NotImplementedError end private
def param_name @options[:param_name].to_s end
def link(text, target, attributes = {}) if target.is_a? Fixnum attributes[:rel] = rel_value(target) target = url(target) end attributes[:href] = target tag(:a, text, attributes) end def tag(name, value, attributes = {}) string_attributes = attributes.inject('') do |attrs, pair| unless pair.last.nil? attrs << %( #{pair.first}="#{CGI::escapeHTML(pair.last.to_s)}") end attrs end "<#{name}#{string_attributes}>#{value}</#{name}>" end
def rel_value(page) case page when @collection.current_page - 1; 'prev' + (page == 1 ? ' start' : '') when @collection.current_page + 1; 'next' when 1; 'start' end end
def symbolized_update(target, other) other.each do |key, value| key = key.to_sym existing = target[key] if value.is_a?(Hash) and (existing.is_a?(Hash) or existing.nil?) symbolized_update(existing || (target[key] = {}), value) else target[key] = value end end end end end end
|