adding sorting by name

This commit is contained in:
Mohammad Naghavi 2016-02-11 17:37:40 +01:00
parent e61c1805cd
commit 89cab0efb3
3 changed files with 76 additions and 30 deletions

View file

@ -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);
});
}
});
}