Skip to content

Commit 2372f39

Browse files
authored
Merge pull request #857 from StApostol/master
Add pagination
2 parents 241100b + 144f9ae commit 2372f39

File tree

7 files changed

+166
-6
lines changed

7 files changed

+166
-6
lines changed

public/css/lfm.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ a {
189189

190190
/* Items */
191191

192-
#content.preserve_actions_space {
192+
#pagination.preserve_actions_space {
193+
padding-top: 1em;
193194
padding-bottom: 4rem; /* preserve space for main actions */
194195
}
195196

public/js/script.js

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ var refreshFoldersAndItems = function (data) {
297297

298298
var hideNavAndShowEditor = function (data) {
299299
$('#nav-buttons > ul').addClass('d-none');
300-
$('#content').html(data).removeClass('preserve_actions_space');
300+
$('#content').html(data);
301+
$('#pagination').removeClass('preserve_actions_space')
301302
clearSelected();
302303
}
303304

@@ -309,20 +310,131 @@ function loadFolders() {
309310
});
310311
}
311312

312-
function loadItems() {
313+
function generatePaginationHTML(el, args) {
314+
var template = '<li class="page-item"><\/li>';
315+
var linkTemplate = '<a class="page-link"><\/a>';
316+
var currentPage = args.currentPage;
317+
var totalPage = args.totalPage;
318+
var rangeStart = args.rangeStart;
319+
var rangeEnd = args.rangeEnd;
320+
var i;
321+
322+
// Disable page range, display all the pages
323+
if (args.pageRange === null) {
324+
for (i = 1; i <= totalPage; i++) {
325+
var button = $(template)
326+
.attr('data-num', i)
327+
.append($(linkTemplate).html(i));
328+
if (i == currentPage) {
329+
button.addClass('active');
330+
}
331+
el.append(button);
332+
}
333+
334+
return;
335+
}
336+
337+
if (rangeStart <= 3) {
338+
for (i = 1; i < rangeStart; i++) {
339+
var button = $(template)
340+
.attr('data-num', i)
341+
.append($(linkTemplate).html(i));
342+
if (i == currentPage) {
343+
button.addClass('active');
344+
}
345+
el.append(button);
346+
}
347+
} else {
348+
var button = $(template)
349+
.attr('data-num', 1)
350+
.append($(linkTemplate).html(1));
351+
el.append(button);
352+
353+
var button = $(template)
354+
.addClass('disabled')
355+
.append($(linkTemplate).html('...'));
356+
el.append(button);
357+
}
358+
359+
for (i = rangeStart; i <= rangeEnd; i++) {
360+
var button = $(template)
361+
.attr('data-num', i)
362+
.append($(linkTemplate).html(i));
363+
if (i == currentPage) {
364+
button.addClass('active');
365+
}
366+
el.append(button);
367+
}
368+
369+
if (rangeEnd >= totalPage - 2) {
370+
for (i = rangeEnd + 1; i <= totalPage; i++) {
371+
var button = $(template)
372+
.attr('data-num', i)
373+
.append($(linkTemplate).html(i));
374+
el.append(button);
375+
}
376+
} else {
377+
var button = $(template)
378+
.addClass('disabled')
379+
.append($(linkTemplate).html('...'));
380+
el.append(button);
381+
382+
var button = $(template)
383+
.attr('data-num', totalPage)
384+
.append($(linkTemplate).html(totalPage));
385+
el.append(button);
386+
}
387+
}
388+
389+
function createPagination(paginationSetting) {
390+
var el = $('<ul class="pagination" role="navigation"></ul>');
391+
392+
var currentPage = paginationSetting.current_page;
393+
var pageRange = 5;
394+
var totalPage = Math.ceil(paginationSetting.total / paginationSetting.per_page);
395+
396+
var rangeStart = currentPage - pageRange;
397+
var rangeEnd = currentPage + pageRange;
398+
399+
if (rangeEnd > totalPage) {
400+
rangeEnd = totalPage;
401+
rangeStart = totalPage - pageRange * 2;
402+
rangeStart = rangeStart < 1 ? 1 : rangeStart;
403+
}
404+
405+
if (rangeStart <= 1) {
406+
rangeStart = 1;
407+
rangeEnd = Math.min(pageRange * 2 + 1, totalPage);
408+
}
409+
410+
generatePaginationHTML(el, {
411+
totalPage: totalPage,
412+
currentPage: currentPage,
413+
pageRange: pageRange,
414+
rangeStart: rangeStart,
415+
rangeEnd: rangeEnd
416+
});
417+
418+
$('#pagination').append(el);
419+
}
420+
421+
function loadItems(page) {
313422
loading(true);
314-
performLfmRequest('jsonitems', {show_list: show_list, sort_type: sort_type}, 'html')
423+
performLfmRequest('jsonitems', {show_list: show_list, sort_type: sort_type, page: page || 1}, 'html')
315424
.done(function (data) {
316425
selected = [];
317426
var response = JSON.parse(data);
318427
var working_dir = response.working_dir;
319428
items = response.items;
320429
var hasItems = items.length !== 0;
430+
var hasPaginator = !!response.paginator;
321431
$('#empty').toggleClass('d-none', hasItems);
322432
$('#content').html('').removeAttr('class');
433+
$('#pagination').html('').removeAttr('class');
323434

324435
if (hasItems) {
325-
$('#content').addClass(response.display).addClass('preserve_actions_space');
436+
$('#content').addClass(response.display);
437+
$('#pagination').addClass('preserve_actions_space');
326438

327439
items.forEach(function (item, index) {
328440
var template = $('#item-template').clone()
@@ -352,6 +464,18 @@ function loadItems() {
352464
});
353465
}
354466

467+
if (hasPaginator) {
468+
createPagination(response.paginator);
469+
470+
$('#pagination a').on('click', function(event) {
471+
event.preventDefault();
472+
473+
loadItems($(this).closest('li')[0].getAttribute('data-num'));
474+
475+
return false;
476+
});
477+
}
478+
355479
$('#nav-buttons > ul').removeClass('d-none');
356480

357481
$('#working_dir').val(working_dir);

src/Controllers/ItemsController.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,20 @@ class ItemsController extends LfmController
1616
*/
1717
public function getItems()
1818
{
19+
$currentPage = self::getCurrentPageFromRequest();
20+
21+
$perPage = $this->helper->getPaginationPerPage();
22+
$items = array_merge($this->lfm->folders(), $this->lfm->files());
23+
1924
return [
2025
'items' => array_map(function ($item) {
2126
return $item->fill()->attributes;
22-
}, array_merge($this->lfm->folders(), $this->lfm->files())),
27+
}, array_slice($items, ($currentPage - 1) * $perPage, $perPage)),
28+
'paginator' => [
29+
'current_page' => $currentPage,
30+
'total' => count($items),
31+
'per_page' => $perPage,
32+
],
2333
'display' => $this->helper->getDisplayMode(),
2434
'working_dir' => $this->lfm->path('working_dir'),
2535
];
@@ -76,4 +86,12 @@ public function domove()
7686

7787
return parent::$success_response;
7888
}
89+
90+
private static function getCurrentPageFromRequest()
91+
{
92+
$currentPage = (int) request()->get('page', 1);
93+
$currentPage = $currentPage < 1 ? 1 : $currentPage;
94+
95+
return $currentPage;
96+
}
7997
}

src/Lfm.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ public function maxUploadSize()
153153
return $this->config->get('lfm.folder_categories.' . $this->currentLfmType() . '.max_size');
154154
}
155155

156+
public function getPaginationPerPage()
157+
{
158+
return $this->config->get("lfm.paginator.perPage", 30);
159+
}
160+
156161
/**
157162
* Check if users are allowed to use their private folders.
158163
*

src/LfmItem.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Str;
66
use Symfony\Component\HttpFoundation\File\UploadedFile;
7+
use Illuminate\Support\Str;
78

89
class LfmItem
910
{

src/config/lfm.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@
7474
],
7575
],
7676

77+
/*
78+
|--------------------------------------------------------------------------
79+
| Pagination
80+
|--------------------------------------------------------------------------
81+
*/
82+
83+
'paginator' => [
84+
'perPage' => 30,
85+
],
86+
7787
/*
7888
|--------------------------------------------------------------------------
7989
| Upload / Validation

src/views/index.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
</div>
9393

9494
<div id="content"></div>
95+
<div id="pagination"></div>
9596

9697
<a id="item-template" class="d-none">
9798
<div class="square"></div>

0 commit comments

Comments
 (0)