From 89cab0efb359c096a22a17990ce7752e2fba3d70 Mon Sep 17 00:00:00 2001 From: Mohammad Naghavi Date: Thu, 11 Feb 2016 17:37:40 +0100 Subject: [PATCH] adding sorting by name --- css/main.css | 11 ++++++++ index.html | 23 +++++++++++++---- js/main.js | 72 ++++++++++++++++++++++++++++++++++------------------ 3 files changed, 76 insertions(+), 30 deletions(-) diff --git a/css/main.css b/css/main.css index 0be25b4..6f1f314 100644 --- a/css/main.css +++ b/css/main.css @@ -7,6 +7,17 @@ a { text-decoration: none; } +.list-sort { + background: #ddd; + border-radius: 1rem; + padding: .5rem 2rem; +} + +.list-sort label { + display: inline-block; + margin: 0 1rem 0 0; +} + .file-list { list-style: none; } diff --git a/index.html b/index.html index 6eabf98..091857a 100644 --- a/index.html +++ b/index.html @@ -16,12 +16,25 @@

Files

+ +
+
sort list
+ + +
+
diff --git a/js/main.js b/js/main.js index 11be9ed..bba13be 100644 --- a/js/main.js +++ b/js/main.js @@ -69,6 +69,47 @@ $(document).ready(function () { return parentDir; } + function renderFileList(filesData, path) { + + var sortBy = $('input[name=sort]:checked').val(); + if (sortBy === "date") { + console.log("sort by date"); + + filesData.sort(function (fileA, fileB) { + return fileB.mtime.getTime() - fileA.mtime.getTime(); + }); + + } else if (sortBy === "name") { + console.log("sort by name"); + + filesData.sort(function (fileA, fileB) { + return fileA.name.toLowerCase().localeCompare(fileB.name.toLowerCase()); + }); + } + + fileListElement.empty(); + + var parentDir = getParentDir(path); + + if (parentDir) { + fileListElement.append(renderFileElement( + parentDir, + "..", + "directory" + )); + } + + filesData.forEach(function (fileData) { + fileListElement.append(renderFileElement( + path, + fileData.name, + fileData.type, + fileData.size, + fileData.mtime + )); + }); + } + function navigateTo(path) { console.log("navigateTo", path); @@ -80,32 +121,13 @@ $(document).ready(function () { return fileData.mtime = new Date(fileData.mtime); }); - filesData.sort(function (fileA, fileB) { - return fileB.mtime.getTime() - fileA.mtime.getTime(); - }); - - fileListElement.empty(); - - var parentDir = getParentDir(path); - - if (parentDir) { - fileListElement.append(renderFileElement( - parentDir, - "..", - "directory" - )); - } - - filesData.forEach(function (fileData) { - fileListElement.append(renderFileElement( - path, - fileData.name, - fileData.type, - fileData.size, - fileData.mtime - )); - }); + renderFileList(filesData, path); + $('input[name=sort]') + .unbind("change") + .on("change", function () { + renderFileList(filesData, path); + }); } }); }