Permalink
Browse files

refactor windowed_page_numbers for speed. this kinda reverts 3c594b. …

…[Lida Tang]
  • Loading branch information...
1 parent 787555f commit c5d532f8f73164f313f455139b31e5f9d5bfcf29 @mislav mislav committed Nov 24, 2008
Showing with 18 additions and 11 deletions.
  1. +18 −11 lib/will_paginate/view_helpers/link_renderer_base.rb
View
29 lib/will_paginate/view_helpers/link_renderer_base.rb
@@ -43,19 +43,26 @@ def windowed_page_numbers
window_to = total_pages if window_to > total_pages
end
- visible = (1..total_pages).to_a
- left_gap = (2 + outer_window)...window_from
- right_gap = (window_to + 1)...(total_pages - outer_window)
-
- # replace page numbers that shouldn't be visible with `:gap`
- [right_gap, left_gap].each do |gap|
- if (gap.last - gap.first) > 1
- visible -= gap.to_a
- visible.insert(gap.first - 1, :gap)
- end
+ # these are always visible
+ middle = window_from..window_to
+
+ # left window
+ if outer_window + 3 < middle.first # there's a gap
+ left = (1..(outer_window + 1)).to_a
+ left << :gap
+ else # runs into visible pages
+ left = 1...middle.first
+ end
+
+ # right window
+ if total_pages - outer_window - 2 > middle.last # again, gap
+ right = ((total_pages - outer_window)..total_pages).to_a
+ right.unshift :gap
+ else # runs into visible pages
+ right = (middle.last + 1)..total_pages
end
- visible
+ left.to_a + middle.to_a + right.to_a
end
private

0 comments on commit c5d532f

Please sign in to comment.