From 28e4f8e705118b512679bfbc176bd9824e79a9c2 Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Thu, 3 Mar 2016 17:11:54 +0100 Subject: [PATCH] Using own custom Foundation 6 paginator --- Gemfile | 3 +- Gemfile.lock | 3 -- app/views/packages/_grid_thumbnail.slim | 9 ++-- app/views/packages/_paginator.slim | 8 ++-- config/application.rb | 2 + lib/foundation_pagination_renderer.rb | 55 +++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 lib/foundation_pagination_renderer.rb diff --git a/Gemfile b/Gemfile index 5cbbba4..6241c01 100644 --- a/Gemfile +++ b/Gemfile @@ -85,7 +85,8 @@ gem 'will_paginate' # Style the paginator properly to use Zurb Foundation's style # https://github.com/acrogenesis/will_paginate-foundation -gem 'will_paginate-foundation' +# (Does not support Foundation 6 yet.) +#gem 'will_paginate-foundation' # Lightbox image viewer for full-sized images gem 'fancybox2-rails', '~> 0.2.8' diff --git a/Gemfile.lock b/Gemfile.lock index d5b6012..13f3129 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -269,8 +269,6 @@ GEM railties (>= 4.0) sprockets-rails (>= 2.0, < 4.0) will_paginate (3.1.0) - will_paginate-foundation (5.3.4) - will_paginate (>= 3.0.3) PLATFORMS ruby @@ -305,7 +303,6 @@ DEPENDENCIES uglifier (>= 1.3.0) web-console (~> 2.0) will_paginate - will_paginate-foundation BUNDLED WITH 1.11.2 diff --git a/app/views/packages/_grid_thumbnail.slim b/app/views/packages/_grid_thumbnail.slim index 92c331f..c07fa5d 100644 --- a/app/views/packages/_grid_thumbnail.slim +++ b/app/views/packages/_grid_thumbnail.slim @@ -2,14 +2,13 @@ a.black href=package_path(name: pkg.name) div.grid-thumbnail - if pkg.screenshots.any? - // TODO: smarter search for the beste screenshot instead of taking the first one + // TODO: smarter selection of the most useful screenshot instead of taking the first one - screenshot = pkg.screenshots.first - = image_tag(screenshot.image.url(:thumb, timestamp: false), alt: screenshot.caption) + = image_tag(screenshot.image.url(:thumb, timestamp: false), alt: screenshot.caption, class: 'thumbnail') - else - img.screenshot src="/images/dummy/no-screenshots-upload-one.svg" + img.screenshot.thumbnail src="/images/dummy/no-screenshots-upload-one.svg" div - // TODO: use description instead of package name (as soon as it is available) .pkgname - =pkg.name + = pkg.name .pkgdescription = pkg.description diff --git a/app/views/packages/_paginator.slim b/app/views/packages/_paginator.slim index 934febb..59f82bb 100644 --- a/app/views/packages/_paginator.slim +++ b/app/views/packages/_paginator.slim @@ -1,8 +1,8 @@ - if @packages.length>0 // Use different pagination navigators depending on the screen width - div.show-for-large-up - =will_paginate @packages, renderer: FoundationPagination::Rails, :inner_window => 3 + div.show-for-large + =will_paginate @packages, :renderer => FoundationPaginationRenderer, :inner_window => 3 div.show-for-medium-only - =will_paginate @packages, renderer: FoundationPagination::Rails, :inner_window => 1 + =will_paginate @packages, :renderer => FoundationPaginationRenderer, :inner_window => 1 div.show-for-small-only - =will_paginate @packages, renderer: FoundationPagination::Rails, :page_links => false + =will_paginate @packages, :renderer => FoundationPaginationRenderer, :page_links => false diff --git a/config/application.rb b/config/application.rb index 029991b..07e0cc3 100644 --- a/config/application.rb +++ b/config/application.rb @@ -12,6 +12,8 @@ module Debshots # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. + config.autoload_paths << Rails.root.join('lib') + # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. # config.time_zone = 'Central Time (US & Canada)' diff --git a/lib/foundation_pagination_renderer.rb b/lib/foundation_pagination_renderer.rb new file mode 100644 index 0000000..bae7513 --- /dev/null +++ b/lib/foundation_pagination_renderer.rb @@ -0,0 +1,55 @@ +include WillPaginate + +class FoundationPaginationRenderer < WillPaginate::ActionView::LinkRenderer + def to_html + list_items = pagination.map do |item| + case item + when Fixnum + page_number(item) + else + send(item) + end + end.join(@options[:link_separator]) + + tag("ul", list_items, class: 'pagination', role: 'navigation', 'aria-label' => 'Pagination') + end + + def container_attributes + super.except(*[:link_options]) + end + + protected + + def page_number(page) + link_options = @options[:link_options] || {} + + if page == current_page + tag :li, page, :class => ('current') + else + tag :li, link(page, page, link_options.merge(:rel => rel_value(page))) + end + end + + def previous_or_next_page(page, text, classname) + link_options = @options[:link_options] || {} + if page + tag :li, link(text, page, link_options), :class => classname + else + tag :li, '', :class => "%s disabled" % classname + end + end + + def gap + tag :li, '', class: 'ellipsis', 'aria-hidden' => 'true' + end + + def previous_page + num = @collection.current_page > 1 && @collection.current_page - 1 + previous_or_next_page(num, '', "pagination-previous") + end + + def next_page + num = @collection.current_page < @collection.total_pages && @collection.current_page + 1 + previous_or_next_page(num, '', "pagination-next") + end +end \ No newline at end of file