Merge pull request #192 from VikasShashidhar/electron-144

Adds a count to the file name in the download manager if the user dow…
This commit is contained in:
Vikas Shashidhar 2017-10-06 13:29:24 +05:30 committed by GitHub
commit 8da10c329a

View File

@ -56,9 +56,10 @@ function showInFinder(id) {
function createDOM(arg) {
if (arg && arg._id) {
let fileDisplayName = getFileDisplayName(arg.fileName);
let downloadItemKey = arg._id;
local.downloadItems.push(arg);
let downloadItemKey = arg._id;
let ul = document.getElementById('download-main');
if (ul) {
@ -108,8 +109,8 @@ function createDOM(arg) {
let h2FileName = document.createElement('h2');
h2FileName.classList.add('text-cutoff');
h2FileName.innerHTML = arg.fileDisplayName;
h2FileName.title = arg.fileDisplayName;
h2FileName.innerHTML = fileDisplayName;
h2FileName.title = fileDisplayName;
fileNameDiv.appendChild(h2FileName);
let fileProgressTitle = document.createElement('span');
@ -195,4 +196,33 @@ function initiate() {
});
}
}
}
/**
* Return a file display name for the download item
*/
function getFileDisplayName(fileName) {
let fileList = local.downloadItems;
let fileNameCount = 0;
let fileDisplayName = fileName;
/* Check if a file with the same name exists
* (akin to the user downloading a file with the same name again)
* in the download bar
*/
for (let i = 0; i < fileList.length; i++) {
if (fileName === fileList[i].fileName) {
fileNameCount++;
}
}
/* If it exists, add a count to the name like how Chrome does */
if (fileNameCount) {
let extLastIndex = fileDisplayName.lastIndexOf('.');
let fileCount = ' (' + fileNameCount + ')';
fileDisplayName = fileDisplayName.slice(0, extLastIndex) + fileCount + fileDisplayName.slice(extLastIndex);
}
return fileDisplayName;
}