126: def paginate(idx, max, url, &b)
127: return '' if max == 0
128: url = url.gsub(%r{^/?(.*?)/?$}, '\1')
129: b ||= lambda{}
130: link = lambda {|*args|
131: i, title, text = args
132: "<a href=\"/#{url}/#{i}#{'?' + @query_string if @query_string}\" " +
133: "#{'onclick="return false;"' if (i == idx)} " +
134: "class=\"#{'disabled ' if (i == idx)}#{b.call(i)}\" " +
135: "title=\"#{title||"Go to page #{i}"}\">#{text||i}</a>"
136: }
137: res = '<div class="nav">'
138: if (idx > 0)
139: res << link.call(idx - 1, "Go to previous page", "« Previous")
140: else
141: res << "<a href=\"/#{url}/0\" onclick=\"return false;\" " +
142: "class=\"disabled\" title=\"Disabled\">« Previous</a>"
143: end
144: if idx < 10
145: idx.times {|i| res << link.call(i)}
146: else
147: (0..2).each {|i| res << link.call(i)}
148: res << ' … '
149: ((idx-4)...idx).each {|i| res << link.call(i)}
150: end
151: res << link.call(idx, 'Current Page')
152: if idx > (max - 10)
153: ((idx+1)...max).each {|i| res << link.call(i)}
154: else
155: ((idx+1)..(idx+4)).each {|i| res << link.call(i)}
156: res << ' … '
157: ((max-3)...max).each {|i| res << link.call(i)}
158: end
159: if (idx < (max - 1))
160: res << link.call(idx + 1, "Go to next page", "Next »")
161: else
162: res << "<a href=\"/#{url}/#{max-1}\" onclick=\"return false;\" " +
163: "class=\"disabled\" title=\"Disabled\"}\">Next »</a>"
164: end
165: res << '</div>'
166: end