Browse Source

implement feed pagination

main
ghost 3 years ago
parent
commit
b247655a5f
  1. 14
      src/application/controller/api/post/get.php
  2. 22
      src/public/css/template/default/module/feed.css
  3. 4
      src/public/js/home.js
  4. 12
      src/public/js/module/feed.js
  5. 4
      src/public/js/people.js

14
src/application/controller/api/post/get.php

@ -4,10 +4,13 @@ $response = [
'success' => false, 'success' => false,
'message' => _('Internal server error'), 'message' => _('Internal server error'),
'posts' => [], 'posts' => [],
'page' => 0,
]; ];
if (isset($_SESSION['userName'])) { if (isset($_SESSION['userName'])) {
$page = isset($_GET['page']) ? Filter::int($_GET['page']) : 1;
$userNames = []; $userNames = [];
if (isset($_GET['userName']) && !empty($_GET['userName'])) { if (isset($_GET['userName']) && !empty($_GET['userName'])) {
@ -22,11 +25,15 @@ if (isset($_SESSION['userName'])) {
} }
} }
if ($result = $_twister->getPosts($userNames, APPLICATION_MAX_POST_FEED)) { if ($result = $_twister->getPosts($userNames, APPLICATION_MAX_POST_FEED * $page)) {
$postsTotal = 0;
$posts = []; $posts = [];
foreach ($result as $post) { foreach ($result as $post) {
// Count posts
$postsTotal++;
// Process reTwists // Process reTwists
$reTwist = []; $reTwist = [];
if ($post['reTwist']) { if ($post['reTwist']) {
@ -50,7 +57,8 @@ if (isset($_SESSION['userName'])) {
$response = [ $response = [
'success' => true, 'success' => true,
'message' => _('Posts successfully loaded'), 'message' => _('Posts successfully loaded'),
'posts' => $posts 'posts' => $posts,
'page' => $postsTotal == $page * APPLICATION_MAX_POST_FEED ? $page + 1 : 0
]; ];
} else { } else {
@ -59,6 +67,7 @@ if (isset($_SESSION['userName'])) {
'success' => false, 'success' => false,
'message' => _('Could not receive post data'), 'message' => _('Could not receive post data'),
'posts' => [], 'posts' => [],
'page' => 0
]; ];
} }
@ -67,6 +76,7 @@ if (isset($_SESSION['userName'])) {
'success' => false, 'success' => false,
'message' => _('Session expired'), 'message' => _('Session expired'),
'posts' => [], 'posts' => [],
'page' => 0
]; ];
} }

22
src/public/css/template/default/module/feed.css

@ -1,3 +1,7 @@
.moduleFeed {
margin-bottom: 36px;
}
.moduleFeed .item { .moduleFeed .item {
padding: 16px; padding: 16px;
margin-bottom: 2px; margin-bottom: 2px;
@ -59,4 +63,22 @@
.moduleFeed .item .message .info a:hover, .moduleFeed .item .message .info a:hover,
.moduleFeed .item .message .info a:visited { .moduleFeed .item .message .info a:visited {
color: #1c1d1e; color: #1c1d1e;
}
.moduleFeed .loadMore {
width: 100%;
margin: 8px 0;
padding: 0 16px;
background: rgba(39, 46, 57, 0.28);
border-radius: 3px;
cursor: pointer;
text-align: center;
height: 36px;
line-height: 36px;
text-align: center;
box-sizing: border-box;
}
.moduleFeed .loadMore:hover {
background-color: #272E39
} }

4
src/public/js/home.js

@ -2,12 +2,12 @@ $(document).ready(function() {
// Init modules // Init modules
ModuleMenu.init('/'); ModuleMenu.init('/');
ModuleFeed.load('#moduleFeed', true); ModuleFeed.load('#moduleFeed', 1, true);
//ModuleUsers.load('#moduleUsers', true); //ModuleUsers.load('#moduleUsers', true);
// Event listeners // Event listeners
$(document).on('ModulePost.add:success', function(/*event, response*/) { $(document).on('ModulePost.add:success', function(/*event, response*/) {
ModuleFeed.load('#moduleFeed', true); ModuleFeed.load('#moduleFeed', 1, true);
}); });
}); });

12
src/public/js/module/feed.js

@ -97,11 +97,12 @@ var ModuleFeed = {
} }
}); });
}, },
load: function(feed, reFresh) { load: function(feed, page, reFresh) {
$.ajax({ $.ajax({
url: 'api/post/get', url: 'api/post/get',
type: 'GET', type: 'GET',
data: { data: {
page: page,
userName: $(feed).data('username') userName: $(feed).data('username')
}, },
success: function (response) { success: function (response) {
@ -116,6 +117,15 @@ var ModuleFeed = {
ModuleFeed.loadAvatar(feed, this.userName); ModuleFeed.loadAvatar(feed, this.userName);
}); });
if (response.page > 0) {
$(feed).append(
$('<div/>', {
'class': 'loadMore',
'onclick': 'ModuleFeed.load(\'' + feed + '\', ' + response.page + ', true)'
}).text('More')
);
}
} else { } else {
console.log(response.message); console.log(response.message);

4
src/public/js/people.js

@ -3,12 +3,12 @@ $(document).ready(function() {
// Init modules // Init modules
ModuleMenu.init('people'); ModuleMenu.init('people');
ModuleFollowing.load('#moduleFollowing', true); ModuleFollowing.load('#moduleFollowing', true);
ModuleFeed.load('#moduleFeed', true); ModuleFeed.load('#moduleFeed', 1, true);
// Event listeners // Event listeners
$(document).on('ModuleFollowing.unFollow:success', function(/*event, response*/) { $(document).on('ModuleFollowing.unFollow:success', function(/*event, response*/) {
ModuleMenu.init('people'); ModuleMenu.init('people');
ModuleFollowing.load('#moduleFollowing', true); ModuleFollowing.load('#moduleFollowing', true);
ModuleFeed.load('#moduleFeed', true); ModuleFeed.load('#moduleFeed', 1, true);
}); });
}); });
Loading…
Cancel
Save