-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add all files #1
base: main
Are you sure you want to change the base?
Changes from 14 commits
61e7d36
96b36a2
3ee28eb
c398ea6
b9f092b
94dc553
f17982c
221f0b4
bed053f
9f075e6
7931232
692355e
d4b4678
75af4e5
c2f1060
b1ce200
a86be5a
6c5ade7
2a08027
a871d98
005fc8d
5284aa6
6e2a02f
da4e84a
ad8a394
bbc0357
b63449c
5566d6a
5dea12a
e54d5dc
a84a25a
6a38d35
728540c
8444cb5
95738ed
2c1e643
b813360
46deff2
f3ae26f
ab68d1a
f5f7136
48157b3
604a830
dde49b4
c6e6cc5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,36 @@ | ||
# FyloCodeReview | ||
# FyloCodeReview | ||
|
||
amosZohar - X | ||
</br> | ||
benda - X | ||
</br> | ||
davidArnfled - X | ||
</br> | ||
evyatarBabay - X | ||
</br> | ||
hinoy - X | ||
</br> | ||
idanBenGavriel - X | ||
</br> | ||
inonEl - X | ||
</br> | ||
liorHassin - X | ||
</br> | ||
natiPorish - X | ||
</br> | ||
orenLevi - X | ||
</br> | ||
rotemNissim - V | ||
</br> | ||
shirYahav - X | ||
</br> | ||
talLevi - V | ||
</br> | ||
yoavFridman - X | ||
</br> | ||
eyarGdolov - V -> Alon/Margol check me before send. | ||
</br> | ||
reyNaftali - X | ||
</br> | ||
omerAmsalem - X | ||
</br> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="description" content="Frontend Mentor - Fylo data storage component"> | ||
<meta name="keywords" content="Fylo data storage component, Frontend Mentor"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link href="styles.css" rel="stylesheet"> | ||
</link> | ||
<link rel="icon" type="image/png" sizes="32x32" href="images/favicon-32x32.png"> | ||
<title>Fylo data storage component</title> | ||
<script src="index.js"></script> | ||
</head> | ||
|
||
<body onload="Initialize()"> | ||
<main> | ||
<!--Fylo Container --> | ||
<section class="fylo-container"> | ||
<img class="logo" src="images/logo.svg" alt="Fylo logo" /> | ||
<section class="icons"> | ||
<div class="icon-img"> | ||
<img src="images/icon-document.svg" alt="" aria-hidden="true" /> | ||
</div> | ||
<div class="icon-img interactive" id="Upload-btn" onclick="openUploadMenu()"> | ||
<img src="images/icon-folder.svg" alt="" aria-hidden="true" /> | ||
</div> | ||
<div class="icon-img"> | ||
<img src="images/icon-upload.svg" alt="" aria-hidden="true" /> | ||
</div>> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the '>' char |
||
<input multiple="true" oninput="onInput(event)" id="upload-input" type="file" class="Invisible"> | ||
</section> | ||
</section> | ||
<!-- Storage Container --> | ||
<section class="storage-container"> | ||
<h1>You've used <span id="used-space">815 GB</span> of your storage</h1> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If JavaScript updates this number, don't display a number here; instead, display "loading..." for example. |
||
<!-- Progress Bar --> | ||
|
||
<section class="progress-bar"> | ||
<div class="gradient-bar"> | ||
<div class="dot-container"> | ||
<div class="dot"></div> | ||
</div> | ||
</div> | ||
</section> | ||
<h2> | ||
<span>0 GB</span> | ||
<span id="max-capacity">1000 GB</span> | ||
</h2> | ||
<!-- GB left --> | ||
<section class="gb-left"> | ||
<p id="remaining-space">185 <span>GB left</span></p> | ||
</section> | ||
<span id="file-names-container" class="uploaded-files_container"></span> | ||
</section> | ||
</main> | ||
</body> | ||
|
||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/*NOTE: the size of the files extracted from the <input type="file"> element | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The storage number in the front dont work. |
||
is measured in Bytes. Thus, all convertions from Bytes, to KiloBytes, MegaBytes | ||
and so on must take that into account. */ | ||
|
||
const KB = 1024; | ||
const MB = KB*KB; | ||
const GB = KB*MB; | ||
|
||
const AVAIL_SPACE = "available_space"; | ||
const size_suffix = "&&fileSize"; | ||
|
||
const total_space = 10*MB;/*measured in Byte units.*/ | ||
|
||
/** | ||
* | ||
* @param {Event} e | ||
* @returns {void} - this body onload event handler updates | ||
* the html elements representing the remaining space. | ||
*/ | ||
function Initialize(e){ | ||
document.getElementById("max-capacity").innerText = size_to_string(total_space); | ||
if(localStorage.getItem(AVAIL_SPACE)== null){/*Means no files were previously uploaded */ | ||
localStorage.setItem(AVAIL_SPACE,total_space); | ||
} | ||
updateHTML(); | ||
/* checks the localstorage for all previously uploaded images */ | ||
for(let i = 0; i<localStorage.length;i++){ | ||
let key = localStorage.key(i); | ||
if(key.split(size_suffix).length == 1 && key !== AVAIL_SPACE ){ | ||
append_file_name(key); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need for comments within a function, clean and readable code speaks for itself. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, it's preferable to use |
||
} | ||
|
||
/** | ||
* | ||
* @param {*} e | ||
* @returns {void} - This is an event handler for clicking the upload files button. | ||
* It triggers the click event for the <input type="file"> element. | ||
*/ | ||
const openUploadMenu = e => document.getElementById("upload-input").click(); | ||
|
||
const supported_extensions = ["jpg","jpeg","png","gif","JPG","JPEG","PNG","GIF"]; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does this const belong? If it's for use throughout the entire file, place it at the top of the file. If it's only relevant to a specific function, put it at the top of that function. |
||
/** | ||
* | ||
* @param {*} e | ||
* @returns {void} This is the event handler for when the user has finished selecting files to upload. | ||
* It checks if the selected files are in a vlid image format, and if there is enough space for them. | ||
* If all is ok, will upload and update the available space. | ||
*/ | ||
function onInput(e){ | ||
const {files} = e.target;/* extracting the FileList object from the Event object e */ | ||
let all_files_valid = true; | ||
let required_space = 0; | ||
/* traversing the FileList object associated with the input element. */ | ||
for(let i = 0; i< files.length;i++){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using a for loop, utilize the |
||
const file_ext = files[i].name.split(".")[1];/* Extracting the file extension */ | ||
if((supported_extensions.includes(file_ext))==false){ | ||
alert(`${file_ext} File format isn't supported`); | ||
all_files_valid = false; | ||
break; | ||
} | ||
else{ | ||
required_space+=files[i].size; | ||
} | ||
} | ||
|
||
if(all_files_valid){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To simplify the if condition and avoid spaghetti code, use:
|
||
let available_space = getAvailableSpace(); | ||
if(available_space < required_space){ | ||
alert("There is not enough space on the disk"); | ||
return; | ||
} | ||
available_space -= required_space; | ||
setAvailableSpace(available_space); | ||
for(let i = 0; i< files.length;i++){ | ||
/*If a file with the same name was already uploaded, I will overwrite it, | ||
since it might be a different file with a different size but with the same name. | ||
Thus, I must free the space taken by it, since the space taken by the new file | ||
will be add to the required_space variable.*/ | ||
let file_size = localStorage.getItem(sizeKey(files[i].name)); | ||
if(file_size){ | ||
removeFile(files[i].name); | ||
updateHTML(); | ||
} | ||
localStorage.setItem(files[i].name,files[i]); | ||
localStorage.setItem(sizeKey(files[i].name),files[i].size); | ||
append_file_name(files[i].name); | ||
} | ||
updateHTML(); | ||
} | ||
|
||
}; | ||
|
||
const sizeKey = fileName => `${fileName}${size_suffix}`; | ||
|
||
/** | ||
* | ||
* @param {string} fileName | ||
* removes the file and its size from the localData, | ||
* updates the available space, and removes the filename HTML element. | ||
*/ | ||
function removeFile(fileName){ | ||
localStorage.removeItem(fileName); | ||
let size = localStorage.getItem(sizeKey(fileName)) | ||
let available_space = getAvailableSpace(); | ||
available_space += parseInt(size); | ||
setAvailableSpace(available_space); | ||
localStorage.removeItem(sizeKey(fileName)); | ||
htmlNode = document.getElementById(fileName); | ||
htmlNode.parentNode.removeChild(htmlNode); | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} fileName | ||
* adds the fileName to the localStorage and adds an HTML element for it. | ||
*/ | ||
function append_file_name(fileName){ | ||
const newNameElement = document.createElement('span'); | ||
newNameElement.className = "uploaded-item icon-img"; | ||
newNameElement.innerHTML = `${fileName} <span class="remove-file-span" id=${fileName}-span>⬜</span>`; | ||
newNameElement.id = fileName; | ||
document.getElementById("file-names-container").append(newNameElement); | ||
document.getElementById(`${fileName}-span`).addEventListener("click",()=>{ | ||
removeFile(fileName); | ||
updateHTML(); | ||
}); | ||
} | ||
|
||
/** | ||
* | ||
* @param {int} size_bytes | ||
* @returns {string} - a string representing the most fitting unit convertion of size_bytes | ||
*/ | ||
function size_to_string(size_bytes){ | ||
if(size_bytes/GB>1){ | ||
return `${(size_bytes/GB).toFixed(1)} GB`; | ||
} | ||
if(size_bytes/MB>1){ | ||
return `${(size_bytes/MB).toFixed(1)} MB`; | ||
} | ||
if(size_bytes/KB>1){ | ||
return `${(size_bytes/KB).toFixed(1)} KB`; | ||
} | ||
return `${size_bytes} Bytes`; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In JavaScript, the convention for function names is camelCase, so it would be |
||
|
||
function getAvailableSpace(){ | ||
return parseInt(localStorage.getItem(AVAIL_SPACE)); | ||
} | ||
|
||
|
||
function setAvailableSpace(num){ | ||
return localStorage.setItem(AVAIL_SPACE,num); | ||
} | ||
|
||
function setBarWidth(){ | ||
let percentage = 100*(total_space-getAvailableSpace())/total_space; | ||
document.getElementsByClassName("gradient-bar")[0].setAttribute("style",`width:${percentage}%`); | ||
} | ||
|
||
function updateUsed(){ | ||
document.getElementById("used-space").innerText = size_to_string(total_space - getAvailableSpace()); | ||
} | ||
|
||
function updateRemaining(){ | ||
const num_and_unit = size_to_string(getAvailableSpace()).split(" "); | ||
document.getElementById("remaining-space").innerHTML = | ||
`${num_and_unit[0]} <span>${num_and_unit[1]} left</span>`; | ||
} | ||
|
||
function updateHTML(){ | ||
setBarWidth(); | ||
updateRemaining(); | ||
updateUsed(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice using of onload