[#369] refactoring; add pagination

This commit is contained in:
Alexander Machehin 2014-05-06 15:51:04 +06:00
parent 8d211e5a75
commit 2ef938a389
7 changed files with 70 additions and 31 deletions

View File

@ -4,14 +4,14 @@ RosaABF.controller('ActivityCtrl', ['$scope', '$http', '$timeout', '$q', '$filte
all: {}, code: {}, tracker: {}, build: {}, wiki: {} };
$scope.tracker_tab = { title: 'activity_menu.tracker', content: [] , active: false,
filter: { all: true, assigned: false, created: false, name: 'all',
all_count: 0, assigned_count: 0, created_count: 0 },
all_count: 0, assigned_count: 0, created_count: 0, closed_count: 0 },
sort: { sort: 'updated', direction: 'desc', updated_class: 'fa-chevron-up' },
status: 'open' };
status: 'open', pagination: { page: 1, total_count: 0 } };
$scope.pull_requests_tab = { title: 'activity_menu.pull_requests', content: [] , active: false,
filter: { all: true, assigned: false, created: false, name: 'all',
all_count: 0, assigned_count: 0, created_count: 0 },
all_count: 0, assigned_count: 0, created_count: 0, closed_count: 0 },
sort: { sort: 'updated', direction: 'desc', updated_class: 'fa-chevron-up' },
status: 'open' };
status: 'open', pagination: { page: 1, total_count: 0 } };
$scope.getContent = function(tab){
var cur_tab = $scope.$eval(tab+'_tab');
@ -30,6 +30,15 @@ RosaABF.controller('ActivityCtrl', ['$scope', '$http', '$timeout', '$q', '$filte
};
};
getIssuesTab = function(kind) {
if(kind === 'tracker') {
return $scope.tracker_tab;
}
else if(kind === 'pull_requests') {
return $scope.pull_requests_tab;
};
};
$scope.getTimeLinefaClass = function(content) {
var template = 'btn-warning fa-question';
@ -96,12 +105,7 @@ RosaABF.controller('ActivityCtrl', ['$scope', '$http', '$timeout', '$q', '$filte
};
$scope.setIssuesFilter = function(kind, issues_filter) {
if(kind === 'tracker') {
var filter = $scope.tracker_tab.filter;
}
else if(kind === 'pull_requests') {
var filter = $scope.pull_requests_tab.filter;
};
var filter = getIssuesTab(kind).filter;
filter.all = false;
filter.assigned = false;
@ -118,6 +122,7 @@ RosaABF.controller('ActivityCtrl', ['$scope', '$http', '$timeout', '$q', '$filte
sort: tab.sort.sort,
direction: tab.sort.direction,
status: tab.status,
page: tab.pagination.page,
format: 'json' });
}
else if($scope.pull_requests_tab.active) {
@ -126,24 +131,24 @@ RosaABF.controller('ActivityCtrl', ['$scope', '$http', '$timeout', '$q', '$filte
sort: tab.sort.sort,
direction: tab.sort.direction,
status: tab.status,
page: tab.pagination.page,
format: 'json' });
};
$http.get(path).then(function(res) {
tab.content = res.data.content;
tab.filter.all_count = res.data.all_count;
tab.filter.assigned_count = res.data.assigned_count;
tab.filter.created_count = res.data.created_count;
tab.content = res.data.content;
tab.filter.all_count = res.data.all_count;
tab.filter.assigned_count = res.data.assigned_count;
tab.filter.created_count = res.data.created_count;
tab.filter.closed_count = res.data.closed_count;
tab.filter.open_count = res.data.open_count;
tab.pagination.page = res.data.page;
tab.pagination.total_items = parseInt(res.data.issues_count, 10);
});
};
$scope.setIssuesSort = function(kind, issues_sort) {
if(kind === 'tracker') {
var tab = $scope.tracker_tab;
}
else if(kind === 'pull_requests') {
var tab = $scope.pull_requests_tab;
};
var tab = getIssuesTab(kind);
if(tab.sort.direction === 'desc') {
tab.sort = { sort: issues_sort, direction: 'asc' };
var sort_class = 'fa-chevron-down';
@ -157,13 +162,13 @@ RosaABF.controller('ActivityCtrl', ['$scope', '$http', '$timeout', '$q', '$filte
};
$scope.setIssuesStatus = function(kind, issues_status) {
if(kind === 'tracker') {
var tab = $scope.tracker_tab;
}
else if(kind === 'pull_requests') {
var tab = $scope.pull_requests_tab;
};
var tab = getIssuesTab(kind);
tab.status = issues_status;
$scope.getIssuesContent();
};
$scope.selectPage = function(kind, page) {
getIssuesTab(kind).pagination.page = page;
$scope.getIssuesContent();
};
}]);

View File

@ -45,6 +45,7 @@ footer {
.offset20 { margin-top: 20px; }
.boffset20 { margin-bottom: 20px; }
.boffset10 { margin-bottom: 10px; }
.loffset5 { margin-left: 5px; }
[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak {
display: none !important;

View File

@ -124,4 +124,10 @@ class ApplicationController < ActionController::Base
def not_found
raise ActionController::RoutingError.new('Not Found')
end
def current_page
params[:page] = 1 if params[:page].to_i < 1
params[:page]
end
end

View File

@ -10,7 +10,7 @@ class HomeController < ApplicationController
@filter = t('feed_menu').has_key?(params[:filter].try(:to_sym)) ? params[:filter].to_sym : :all
@activity_feeds = current_user.activity_feeds
@activity_feeds = @activity_feeds.where(kind: "ActivityFeed::#{@filter.upcase}".constantize) unless @filter == :all
@activity_feeds = @activity_feeds.paginate page: params[:page]
@activity_feeds = @activity_feeds.paginate page: current_page
respond_to do |format|
format.html { request.xhr? ? render('_list', layout: false) : render('activity') }
@ -45,7 +45,7 @@ class HomeController < ApplicationController
@issues = @all_issues
end
@filter = params[:filter]
@opened_issues, @closed_issues = @issues.not_closed_or_merged.count, @issues.closed_or_merged.count
@opened_issues, @closed_issues = @issues.not_closed_or_merged, @issues.closed_or_merged
@status = params[:status] == 'closed' ? :closed : :open
@issues = @issues.send( (@status == :closed) ? :closed_or_merged : :not_closed_or_merged )
@ -54,7 +54,7 @@ class HomeController < ApplicationController
@direction = params[:direction] == 'asc' ? :asc : :desc
@issues = @issues.order("issues.#{@sort}_at #{@direction}")
.includes(:assignee, :user, :pull_request).uniq
.paginate per_page: 20, page: params[:page]
.paginate page: current_page
respond_to do |format|
format.html { request.xhr? ? render('issues', layout: 'with_sidebar') : render('issues', layout: 'application') }

View File

@ -1,6 +1,7 @@
class Issue < ActiveRecord::Base
include Feed::Issue
STATUSES = ['open', 'closed']
self.per_page = 20
belongs_to :project
belongs_to :user

View File

@ -11,7 +11,9 @@
.col-md-9.col-sm-8.offset20
%tabset.boffset10
- %w(open closed).each do |status|
%tab{ 'heading' => t("layout.issues.statuses.#{status}"), 'active' => "#{kind}_tab.status.#{status}.active",
- count_issues = "({{#{kind}_tab.filter.#{status}_count}})"
%tab{ 'heading' => "#{t "layout.issues.statuses.#{status}"} #{count_issues}",
'active' => "#{kind}_tab.status.#{status}.active",
'ng-click' => "setIssuesStatus('#{kind}', '#{status}')" }
.pull-right.boffset10
@ -23,8 +25,11 @@
%tr{ 'ng-repeat' => "issue in #{kind}_tab.content" }
%td
%a{ 'ng-href' => "{{issue.issue_url}}" }
%span.text-muted {{issue.project_name}}
%span.text-info {{issue.project_name}}
{{issue.title}}
%span.label.small.loffset5{ 'ng-repeat' => "label in issue.labels",
'ng-style' => "{background: label.color}" }
{{label.name}}
.small
= t 'layout.issues.created_by'
%a{ 'ng-href' => "{{issue.user.link}}" } {{issue.user.uname}}
@ -40,3 +45,14 @@
%a{ 'ng-href' => '{{issue.assignee.link}}',
'title' => "#{t('layout.issues.assigned_to')} {{issue.assignee.fullname}}" }
%img{ 'ng-src' => '{{issue.assignee.image}}' }
%pagination.pagination-sm{ 'boundary-links' => 'true',
'total-items' => "#{kind}_tab.pagination.total_items",
'page' => "#{kind}_tab.pagination.page",
'previous-text' => "", 'next-text' => "",
'first-text' => "«", "last-text" => "»",
'items-per-page' => Issue.per_page,
'max-size' => 5,
'rotate' => 'false',
'ng-show' => "#{kind}_tab.pagination.total_items > #{Issue.per_page}",
'on-select-page' => "selectPage('#{kind}', page)" }

View File

@ -1,6 +1,10 @@
json.all_count @all_issues.not_closed_or_merged.count
json.open_count @opened_issues.count
json.closed_count @closed_issues.count
json.assigned_count @assigned_issues.not_closed_or_merged.count
json.created_count @created_issues.not_closed_or_merged.count
json.page params[:page]
json.issues_count @issues.count
json.content do
json.array!(@issues) do |issue|
@ -19,5 +23,11 @@ json.content do
json.image avatar_url(issue.assignee, :micro) if issue.assignee
json.fullname issue.assignee.fullname if issue.assignee
end
json.labels do
json.array!(issue.labels) do |label|
json.name label.name
json.color "##{label.color}"
end
end
end
end