From 652a00faf5d9cb77f2f9b91c7c19aaf86e10ff5d Mon Sep 17 00:00:00 2001 From: OldHawk Date: Wed, 26 Apr 2017 13:06:05 +0800 Subject: [PATCH] add torrents list view pagination --- .../controllers/home.client.controller.js | 2 +- modules/core/client/less/mt.less | 6 + .../torrent-info.client.controller.js | 5 +- .../controllers/torrents.client.controller.js | 67 ++++++---- .../client/views/movie-list.client.view.html | 121 ++++++++++-------- .../controllers/torrents.server.controller.js | 41 ++++-- 6 files changed, 150 insertions(+), 92 deletions(-) diff --git a/modules/core/client/controllers/home.client.controller.js b/modules/core/client/controllers/home.client.controller.js index 2ced8b47..450f6237 100644 --- a/modules/core/client/controllers/home.client.controller.js +++ b/modules/core/client/controllers/home.client.controller.js @@ -51,7 +51,7 @@ */ vm.getMovieTopInfo = function () { vm.moviesInfo = TorrentsService.query({ - limit: 8 + limit: 16 }, function (items) { vm.movieTopList = items; }, function (err) { diff --git a/modules/core/client/less/mt.less b/modules/core/client/less/mt.less index de4f4fc5..4f2c3c3d 100644 --- a/modules/core/client/less/mt.less +++ b/modules/core/client/less/mt.less @@ -388,4 +388,10 @@ color: @brand-danger; } } +} + +.torrent-list { + .pagination-div { + border-top: 2px solid lighten(@gray-base, 70%) + } } \ No newline at end of file diff --git a/modules/torrents/client/controllers/torrent-info.client.controller.js b/modules/torrents/client/controllers/torrent-info.client.controller.js index 5252d25c..d1bf1e13 100644 --- a/modules/torrents/client/controllers/torrent-info.client.controller.js +++ b/modules/torrents/client/controllers/torrent-info.client.controller.js @@ -6,12 +6,11 @@ .controller('TorrentsInfoController', TorrentsInfoController); TorrentsInfoController.$inject = ['$scope', '$state', '$stateParams', '$translate', 'Authentication', 'Notification', 'TorrentsService', - 'MeanTorrentConfig', 'DownloadService', '$sce', '$filter', 'CommentsService', 'ModalConfirmService', 'marked', 'Upload', '$timeout', '$uiViewScroll', + 'MeanTorrentConfig', 'DownloadService', '$sce', '$filter', 'CommentsService', 'ModalConfirmService', 'marked', 'Upload', '$timeout', 'SubtitlesService']; function TorrentsInfoController($scope, $state, $stateParams, $translate, Authentication, Notification, TorrentsService, MeanTorrentConfig, - DownloadService, $sce, $filter, CommentsService, ModalConfirmService, marked, Upload, $timeout, SubtitlesService, - $uiViewScroll) { + DownloadService, $sce, $filter, CommentsService, ModalConfirmService, marked, Upload, $timeout, SubtitlesService) { var vm = this; vm.user = Authentication.user; vm.announce = MeanTorrentConfig.meanTorrentConfig.announce; diff --git a/modules/torrents/client/controllers/torrents.client.controller.js b/modules/torrents/client/controllers/torrents.client.controller.js index bcc89bb0..e23b2430 100644 --- a/modules/torrents/client/controllers/torrents.client.controller.js +++ b/modules/torrents/client/controllers/torrents.client.controller.js @@ -19,10 +19,8 @@ vm.searchTags = []; vm.searchKey = ''; - vm.currPageNumber = 1; - vm.topNumber = 6; - vm.pageNumber = 50; vm.releaseYear = undefined; + vm.topItems = 6; vm.torrentTabs = []; @@ -31,14 +29,40 @@ $state.go('authentication.signin'); } + //page init + vm.torrentBuildPager = torrentBuildPager; + vm.torrentFigureOutItemsToDisplay = torrentFigureOutItemsToDisplay; + vm.torrentPageChanged = torrentPageChanged; + + function torrentBuildPager() { + vm.torrentPagedItems = []; + vm.torrentItemsPerPage = 8; + vm.torrentCurrentPage = 1; + vm.torrentFigureOutItemsToDisplay(); + } + + function torrentFigureOutItemsToDisplay() { + vm.getMoviePageInfo(vm.torrentCurrentPage, function (items) { + vm.torrentFilterLength = items.total; + vm.torrentPagedItems = items.rows; + }); + } + + function torrentPageChanged() { + var element = angular.element('#top_of_torrent_list'); + + vm.torrentFigureOutItemsToDisplay(); + window.scrollTo(0, element[0].offsetTop - 60); + } + /** * getMovieTopInfo */ vm.getMovieTopInfo = function () { - TorrentsService.query({ - limit: vm.topNumber + TorrentsService.get({ + limit: vm.topItems }, function (items) { - vm.movieTopInfo = items; + vm.movieTopInfo = items.rows; }, function (err) { Notification.error({ message: ' ' + $translate.instant('TOP_MOVIE_INFO_ERROR') @@ -68,7 +92,7 @@ }); } e.blur(); - vm.getMoviePageInfo(1); + vm.torrentBuildPager(); }; /** @@ -84,7 +108,7 @@ } else { vm.searchTags.splice(vm.searchTags.indexOf(n), 1); } - vm.getMoviePageInfo(1); + vm.torrentBuildPager(); }; /** @@ -93,7 +117,7 @@ */ vm.onKeysKeyDown = function (evt) { if (evt.keyCode === 13) { - vm.getMoviePageInfo(1); + vm.torrentBuildPager(); } }; @@ -101,31 +125,30 @@ * getMoviePageInfo * @param p: page number */ - vm.getMoviePageInfo = function (p) { - vm.currPageNumber = p; - + vm.getMoviePageInfo = function (p, callback) { //if searchKey or searchTags has value, the skip=0 - var skip = vm.topNumber; - if (vm.searchKey.trim().length > 0 || vm.searchTags.length > 0) { + var skip = vm.topItems; + if (vm.searchKey.trim().length > 0 || vm.searchTags.length > 0 || vm.releaseYear) { skip = 0; } - TorrentsService.query({ - skip: (p - 1) * vm.pageNumber + 0, - limit: p * vm.pageNumber, - keys: vm.searchKey, + TorrentsService.get({ + skip: (p - 1) * vm.torrentItemsPerPage + 0, //skip + limit: vm.torrentItemsPerPage, + keys: vm.searchKey.trim(), torrent_status: 'reviewed', torrent_type: 'movie', torrent_release: vm.releaseYear, torrent_tags: vm.searchTags }, function (items) { - vm.moviePageInfo = items; if (items.length === 0) { Notification.error({ message: ' ' + $translate.instant('MOVIE_PAGE_INFO_EMPTY') }); + } else { + callback(items); + console.log(items); } - console.log(items); }, function (err) { Notification.error({ message: ' ' + $translate.instant('MOVIE_PAGE_INFO_ERROR') @@ -170,7 +193,7 @@ vm.searchTags = []; $('.btn-tag').removeClass('btn-success').addClass('btn-default'); - vm.getMoviePageInfo(1); + vm.torrentBuildPager(); }; /** @@ -193,7 +216,7 @@ } else { vm.releaseYear = y; } - vm.getMoviePageInfo(1); + vm.torrentBuildPager(); }; /** diff --git a/modules/torrents/client/views/movie-list.client.view.html b/modules/torrents/client/views/movie-list.client.view.html index 602a47f2..1675eec9 100644 --- a/modules/torrents/client/views/movie-list.client.view.html +++ b/modules/torrents/client/views/movie-list.client.view.html @@ -1,6 +1,8 @@ -
+
+
+
-
- - - - - - - - - - - - - - + + + +
{{ 'TABLE_FIELDS.INFO' | translate}}{{ 'TABLE_FIELDS.VOTES' | translate}}{{ 'TABLE_FIELDS.LIFETIME' | translate}}{{ 'TABLE_FIELDS.SIZE' | translate}}{{ 'TABLE_FIELDS.SEEDS_LEECHERS_FINISHED' | translate}}{{ 'TABLE_FIELDS.PUBLISHER' | translate}}
-
-
- ... -
-
-
{{item.torrent_original_title}} - / {{item.torrent_title}} +
+
+ + + + + + + + + + + + + + - + - - - + + + - - - -
{{ 'TABLE_FIELDS.INFO' | translate}}{{ 'TABLE_FIELDS.VOTES' | translate}}{{ 'TABLE_FIELDS.LIFETIME' | translate}}{{ 'TABLE_FIELDS.SIZE' | translate}}{{ 'TABLE_FIELDS.SEEDS_LEECHERS_FINISHED' | translate}}{{ 'TABLE_FIELDS.PUBLISHER' | translate}}
+
+
+ ... +
+
+
{{item.torrent_original_title}} + / {{item.torrent_title}} + title="{{ 'TITLE_ALT.DOWNLOAD_TORRENT' | translate}}" + ng-click="vm.downloadTorrent(item._id); $event.stopPropagation();"> {{ 'CA_DOWNLOAD' | translate}} -
+ -
- {{t}} -
+
+ {{t}} +
-
{{item.torrent_filename | filename}}
+
{{item.torrent_filename | filename}}
-
- +
+ {{ item.torrent_release}} @@ -143,35 +146,41 @@ {{ 'RESOURCESTAGS.' + vm.getTagTitle(t) + '.' + t | translate}} +
-
-
+ IMDB {{item.torrent_imdb_votes | number : 1}} - {{item.createdat | life}}{{item.torrent_size | bytes:2}}{{item.createdat | life}}{{item.torrent_size | bytes:2}}{{item.user.displayName}}
+

+ + {{item.torrent_finished}} +

+
{{item.user.displayName}}
+
+ +
+ +
\ No newline at end of file diff --git a/modules/torrents/server/controllers/torrents.server.controller.js b/modules/torrents/server/controllers/torrents.server.controller.js index 950aff8e..e6c61a9b 100644 --- a/modules/torrents/server/controllers/torrents.server.controller.js +++ b/modules/torrents/server/controllers/torrents.server.controller.js @@ -393,20 +393,41 @@ exports.list = function (req, res) { console.log(JSON.stringify(condition)); - Torrent.find(condition) - .sort('-createdat') - .populate('user', 'displayName') - .skip(skip) - .limit(limit) - .exec(function (err, torrents) { + + var countQuery = function (callback) { + Torrent.count(condition, function (err, count) { if (err) { - return res.status(422).send({ - message: errorHandler.getErrorMessage(err) - }); + callback(err, null); } else { - res.json(torrents); + callback(null, count); } }); + }; + + var findQuery = function (callback) { + Torrent.find(condition) + .sort('-createdat') + .populate('user', 'displayName') + .skip(skip) + .limit(limit) + .exec(function (err, torrents) { + if (err) { + callback(err, null); + } else { + callback(null, torrents); + } + }); + }; + + async.parallel([countQuery, findQuery], function (err, results) { + if (err) { + return res.status(422).send({ + message: 'Torrents query error' + }); + } else { + res.json({rows: results[1], total: results[0]}); + } + }); }; /**