From 530523611cf75c87374d4d130b816c5a914b1be6 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Thu, 30 Oct 2014 19:19:55 +0500 Subject: [PATCH] [#369] projects page: fix paginate --- .../controllers/projects_controller.js | 2 +- app/helpers/paginate_helper.rb | 29 ++++++------- app/views/projects/projects/index.html.haml | 6 +-- .../projects/projects/index.json.jbuilder | 2 +- .../shared/_angularjs_paginate.html.slim | 14 +++++++ .../shared/_angularjs_will_paginate.html.slim | 20 --------- app/views/shared/_paginate.html.haml | 10 ----- lib/ext/angularjs_link_renderer.rb | 41 ------------------- 8 files changed, 29 insertions(+), 95 deletions(-) create mode 100644 app/views/shared/_angularjs_paginate.html.slim delete mode 100644 app/views/shared/_angularjs_will_paginate.html.slim delete mode 100644 app/views/shared/_paginate.html.haml delete mode 100644 lib/ext/angularjs_link_renderer.rb diff --git a/app/assets/javascripts/angular-new/controllers/projects_controller.js b/app/assets/javascripts/angular-new/controllers/projects_controller.js index 8889af48f..2960f27bd 100644 --- a/app/assets/javascripts/angular-new/controllers/projects_controller.js +++ b/app/assets/javascripts/angular-new/controllers/projects_controller.js @@ -16,7 +16,7 @@ RosaABF.controller('ProjectsCtrl', ['$scope', '$http', function($scope, $http) { users: $scope.filter_users, groups: $scope.filter_groups }; $http.get(Routes.projects_path(params)).then(function(res) { $scope.page = res.data.page; - $scope.total_items = parseInt(res.data.projects_count, 10); + $scope.total_items = res.data.projects_count; $scope.projects = res.data.projects; }); }; diff --git a/app/helpers/paginate_helper.rb b/app/helpers/paginate_helper.rb index 569ae2e88..5e1e8968f 100644 --- a/app/helpers/paginate_helper.rb +++ b/app/helpers/paginate_helper.rb @@ -19,23 +19,18 @@ module PaginateHelper super *[collection_or_options, options].compact end - def angularjs_will_paginate(collection_or_options = nil, options = {}) - if collection_or_options.is_a? Hash - options, collection_or_options = collection_or_options, nil - end - options.merge!(renderer: AngularjsLinkRenderer) unless options[:renderer] - options.merge!(next_label: I18n.t('datatables.next_label')) unless options[:next_label] - options.merge!(previous_label: I18n.t('datatables.previous_label')) unless options[:previous_label] - will_paginate *[collection_or_options, options].compact - end + def angularjs_paginate(options = {}) + return if options[:per_page].blank? - def paginate(options) - return nil if options.blank? - render 'shared/paginate', - total_items: options[:total_items], - page: options[:page], - per_page: options[:per_page], - ng_show: options[:ng_show], - select_page: options[:select_page] + options.reverse_merge!( + { + total_items: 'total_items', + page: 'page', + ng_show: "total_items > #{options[:per_page]}", + select_page: "goToPage(page)" + } + ) + + render 'shared/angularjs_paginate', options end end diff --git a/app/views/projects/projects/index.html.haml b/app/views/projects/projects/index.html.haml index 5d875d4ae..8c3695afc 100644 --- a/app/views/projects/projects/index.html.haml +++ b/app/views/projects/projects/index.html.haml @@ -37,8 +37,4 @@ %span.fa.fa-lg.fa-times.text-danger{ 'confirmed-click' => 'leave_project(item)', 'ng-show' => 'item.can_leave_project', 'ng-confirm-click' => t('layout.confirm') } - = paginate( total_items: 'total_items', - page: 'page', - per_page: Project.per_page, - ng_show: "total_items > #{Project.per_page}", - select_page: "goToPage(page)" ) + = angularjs_paginate per_page: Project.per_page diff --git a/app/views/projects/projects/index.json.jbuilder b/app/views/projects/projects/index.json.jbuilder index 27e451353..6305340f3 100644 --- a/app/views/projects/projects/index.json.jbuilder +++ b/app/views/projects/projects/index.json.jbuilder @@ -13,5 +13,5 @@ json.projects do end end -json.page params[:page] +json.page params[:page] json.projects_count @projects_count diff --git a/app/views/shared/_angularjs_paginate.html.slim b/app/views/shared/_angularjs_paginate.html.slim new file mode 100644 index 000000000..eb6ba2bdf --- /dev/null +++ b/app/views/shared/_angularjs_paginate.html.slim @@ -0,0 +1,14 @@ +/ Add to controller something like: +/ $scope.goToPage = (number) -> +/ $location.search('page', number) + +pagination[ boundary-links = 'true' + total-items = total_items + ng-model = page + previous-text = "‹" next-text = "›" + first-text = "«" last-text = "»" + items-per-page = per_page + max-size = 5 + rotate = 'false' + ng-show = ng_show + ng-change = select_page ] diff --git a/app/views/shared/_angularjs_will_paginate.html.slim b/app/views/shared/_angularjs_will_paginate.html.slim deleted file mode 100644 index 6ca9b02f6..000000000 --- a/app/views/shared/_angularjs_will_paginate.html.slim +++ /dev/null @@ -1,20 +0,0 @@ -/ Add to controller something like: -/ $scope.goToPage = (number) -> -/ $location.search('page', number) -ul.pagination ng-show='pages' - li ng-class="{'disabled': !page.active}" ng-repeat='page in pages' ng-switch='page.type' - a ng-switch-when='previous_page' ng-hide='page.active' - = t('datatables.previous_label') - a ng-switch-when='previous_page' ng-show='page.active' ng-click='goToPage(page.number)' - = t('datatables.previous_label') - a ng-switch-when='first' - | {{page.number}} - a ng-switch-when='page' ng-click='goToPage(page.number)' - | {{page.number}} - a ng-switch-when='more' ng-click='goToPage(page.number)' … - a ng-switch-when='last' ng-click='goToPage(page.number)' - | {{page.number}} - a ng-switch-when='next_page' ng-hide='page.active' - = t('datatables.next_label') - a ng-switch-when='next_page' ng-show='page.active' ng-click='goToPage(page.number)' - = t('datatables.next_label') diff --git a/app/views/shared/_paginate.html.haml b/app/views/shared/_paginate.html.haml deleted file mode 100644 index 49ba6ccbf..000000000 --- a/app/views/shared/_paginate.html.haml +++ /dev/null @@ -1,10 +0,0 @@ -%pagination{ 'boundary-links' => 'true', - 'total-items' => total_items, - 'page' => page, - 'previous-text' => "‹", 'next-text' => "›", - 'first-text' => "«", "last-text" => "»", - 'items-per-page' => per_page, - 'max-size' => 5, - 'rotate' => 'false', - 'ng-show' => ng_show, - 'on-select-page' => select_page } diff --git a/lib/ext/angularjs_link_renderer.rb b/lib/ext/angularjs_link_renderer.rb deleted file mode 100644 index c766a5b51..000000000 --- a/lib/ext/angularjs_link_renderer.rb +++ /dev/null @@ -1,41 +0,0 @@ -class Array - def html_safe - self - end -end - -class AngularjsLinkRenderer < WillPaginate::ActionView::LinkRenderer - - def to_html - pagination.map do |item| - item.is_a?(Fixnum) ? page_number(item) : send(item) - end - end - - protected - - def page_number(page) - unless page == current_page - {active: true, number: page, type: :page} - else - {active: false, number: page, type: :first} - end - end - - def gap - nil - end - - def next_page - num = @collection.current_page < @collection.total_pages && @collection.current_page + 1 - previous_or_next_page(num, @options[:next_label], :next_page) - end - - def previous_or_next_page(page, text, classname) - if page - {active: true, number: page, type: classname} - else - {active: false, number: page, type: classname} - end - end -end