Skip to content

Commit db1aa30

Browse files
committed
Update directory and file sort by time or alphabets
1 parent 84d0436 commit db1aa30

File tree

6 files changed

+89
-5
lines changed

6 files changed

+89
-5
lines changed

public/js/script.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var show_list;
2+
var sort_type = 0;
23

34
$(document).ready(function () {
45
bootbox.setDefaults({locale:lang['locale-bootbox']});
@@ -74,6 +75,16 @@ $('#list-display').click(function () {
7475
loadItems();
7576
});
7677

78+
$('#list-sort-alpha').click(function() {
79+
sort_type = 0;
80+
loadItems();
81+
});
82+
83+
$('#list-sort-time').click(function() {
84+
sort_type = 1;
85+
loadItems();
86+
});
87+
7788
// ======================
7889
// == Folder actions ==
7990
// ======================
@@ -150,7 +161,7 @@ function loadFolders() {
150161
}
151162

152163
function loadItems() {
153-
performLfmRequest('jsonitems', {show_list: show_list}, 'html')
164+
performLfmRequest('jsonitems', {show_list: show_list, sort_type: sort_type}, 'html')
154165
.done(function (data) {
155166
var response = JSON.parse(data);
156167
$('#content').html(response.html);

src/controllers/ItemsController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ class ItemsController extends LfmController
1616
public function getItems()
1717
{
1818
$path = $this->getCurrentPath();
19+
$sort_type = request('sort_type');
1920

2021
return [
2122
'html' => (string)view($this->getView())->with([
22-
'files' => $this->getFilesWithInfo($path),
23-
'directories' => $this->getDirectories($path)
23+
'files' => $this->getFilesWithInfo($path, $sort_type),
24+
'directories' => $this->getDirectories($path, $sort_type)
2425
]),
2526
'working_dir' => $this->getInternalPath($path)
2627
];

src/lang/en/lfm.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
'nav-upload' => 'Upload',
77
'nav-thumbnails' => 'Thumbnails',
88
'nav-list' => 'List',
9+
'nav-sort' => 'Sort',
10+
'nav-sort-alpha' => 'Sort By Alphabets',
11+
'nav-sort-time' => 'Sort By Time',
912

1013
'menu-rename' => 'Rename',
1114
'menu-delete' => 'Delete',

src/lang/zh-TW/lfm.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
'nav-upload' => '上傳檔案',
77
'nav-thumbnails' => '縮圖顯示',
88
'nav-list' => '列表顯示',
9+
'nav-sort' => '排序',
10+
'nav-sort-alpha' => '依字母排序',
11+
'nav-sort-time' => '依時間排序',
912

1013
'menu-rename' => '重新命名',
1114
'menu-delete' => '刪除',

src/traits/LfmHelpers.php

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public function enabledShareFolder()
236236
*** File System ***
237237
****************************/
238238

239-
public function getDirectories($path)
239+
public function getDirectories($path, $sort_type = 0)
240240
{
241241
$thumb_folder_name = config('lfm.thumb_folder_name');
242242
$all_directories = File::directories($path);
@@ -249,15 +249,22 @@ public function getDirectories($path)
249249
if ($directory_name !== $thumb_folder_name) {
250250
$arr_dir[] = (object)[
251251
'name' => $directory_name,
252+
'updated' => filemtime($directory),
252253
'path' => $this->getInternalPath($directory)
253254
];
254255
}
255256
}
256257

258+
if ($sort_type == 0) {
259+
uasort($arr_dir, array($this, 'cmpDirAlpha'));
260+
} elseif ($sort_type == 1) {
261+
uasort($arr_dir, array($this, 'cmpDirTime'));
262+
}
263+
257264
return $arr_dir;
258265
}
259266

260-
public function getFilesWithInfo($path)
267+
public function getFilesWithInfo($path, $sort_type = 0)
261268
{
262269
$arr_files = [];
263270

@@ -292,6 +299,12 @@ public function getFilesWithInfo($path)
292299
];
293300
}
294301

302+
if ($sort_type == 0) {
303+
uasort($arr_files, array($this, 'cmpAlpha'));
304+
} elseif ($sort_type == 1) {
305+
uasort($arr_files, array($this, 'cmpTime'));
306+
}
307+
295308
return $arr_files;
296309
}
297310

@@ -318,6 +331,42 @@ public function fileIsImage($file)
318331
return starts_with($mime_type, 'image');
319332
}
320333

334+
private static function cmpDirAlpha($a, $b)
335+
{
336+
$cmp = strcmp($a->name, $b->name);
337+
338+
if ($cmp == 0)
339+
return 0;
340+
341+
return ($cmp > 0) ? 1 : -1;
342+
}
343+
344+
private static function cmpDirTime($a, $b)
345+
{
346+
if ($a->updated == $b->updated)
347+
return 0;
348+
349+
return ($a->updated > $b->updated) ? 1 : -1;
350+
}
351+
352+
private static function cmpAlpha($a, $b)
353+
{
354+
$cmp = strcmp($a['name'], $b['name']);
355+
356+
if ($cmp == 0)
357+
return 0;
358+
359+
return ($cmp > 0) ? 1 : -1;
360+
}
361+
362+
private static function cmpTime($a, $b)
363+
{
364+
if ($a['updated'] == $b['updated'])
365+
return 0;
366+
367+
return ($a['updated'] > $b['updated']) ? 1 : -1;
368+
}
369+
321370

322371
/****************************
323372
*** Miscellaneouses ***

src/views/index.blade.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@
6262
<i class="fa fa-list"></i> {{ trans('laravel-filemanager::lfm.nav-list') }}
6363
</a>
6464
</li>
65+
<li class="dropdown">
66+
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
67+
{{ trans('laravel-filemanager::lfm.nav-sort') }} <span class="caret"></span>
68+
</a>
69+
<ul class="dropdown-menu">
70+
<li>
71+
<a href="#" id="list-sort-alpha">
72+
<i class="fa fa-sort-alpha-asc"></i> {{ trans('laravel-filemanager::lfm.nav-sort-alpha') }}
73+
</a>
74+
</li>
75+
<li>
76+
<a href="#" id="list-sort-time">
77+
<i class="fa fa-sort-amount-asc"></i> {{ trans('laravel-filemanager::lfm.nav-sort-time') }}
78+
</a>
79+
</li>
80+
</ul>
81+
</li>
6582
</ul>
6683
</div>
6784
</nav>

0 commit comments

Comments
 (0)