Files
nodejs-fm/static/base.js

148 lines
4.2 KiB
JavaScript
Raw Normal View History

2019-05-21 19:37:11 +05:30
2019-05-24 14:41:30 +05:30
let currDir = {'loc':'','contents':null}
let currSel = {'loc':null,'name':null}
2019-05-21 19:37:11 +05:30
2019-05-23 20:23:32 +05:30
function doUpdate(ele,isDir=false){
//console.log(ele.attr('data-choice'));
2019-05-22 17:09:45 +05:30
if(ele.hasClass('file-isDir')){
2019-05-24 14:41:30 +05:30
currDir.loc = ele.attr('data-choice')
2019-05-22 17:09:45 +05:30
populateContents();
}
2019-05-23 20:23:32 +05:30
if(!isDir){
2019-05-24 14:41:30 +05:30
currSel.loc = ele.attr('data-choice')
$('.nav-bottom-text').html(currSel.name = ele.html())
2019-05-23 20:23:32 +05:30
}
2019-05-22 17:09:45 +05:30
}
2019-05-21 20:31:41 +05:30
function updateContents(contents){
2019-05-23 20:23:32 +05:30
//console.log(contents)
2019-05-22 20:22:59 +05:30
// Change top header contents
2019-05-24 11:04:51 +05:30
$('#files-location').html(contents.loc)
2019-05-22 20:22:59 +05:30
// if empty, return null, this shouldnt execute if the server is responding properly but ok
if(contents===null) {
$('#files-table').append(`<tr><td>null</td><td class="file-handlers"></td></tr>`)
}
2019-05-21 20:31:41 +05:30
else
2019-05-22 17:09:45 +05:30
{
2019-05-23 20:23:32 +05:30
$('#files-table').hide().empty();
2019-05-22 17:09:45 +05:30
contents.contents.forEach(element => {
2019-05-23 20:23:32 +05:30
$('#files-table').append(`<tr class="files-row box-shadow-1-active"><td onclick="doUpdate($(this),${element.isDir})" class="file-name ${(element.isDir?'file-isDir':'file-isFile')}" data-choice="${element.path}">${element.name}</td></tr>`)
2019-05-22 17:09:45 +05:30
});
2019-05-22 20:22:59 +05:30
if(contents.back!=null){
2019-05-24 21:38:31 +05:30
$('#files-table').prepend(`<tr class="files-row box-shadow-1-active"><td onclick="doUpdate($(this),true)" class="file-name file-isDir file-isBack" data-choice="${contents.back}">..</td></tr>`)
2019-05-22 20:22:59 +05:30
}
2019-05-23 20:23:32 +05:30
$('#files-table').fadeIn()
2019-05-22 17:09:45 +05:30
}
2019-05-21 20:31:41 +05:30
}
2019-05-21 19:37:11 +05:30
//set table details
2019-05-21 20:31:41 +05:30
function populateContents(){
2019-05-22 17:09:45 +05:30
$.ajax('/files/ls',{
2019-05-21 20:31:41 +05:30
method:'post',
data: currDir,
success:(msg)=>{
updateContents(msg)
}
2019-05-21 19:37:11 +05:30
2019-05-21 20:31:41 +05:30
})
return null;
2019-05-21 19:37:11 +05:30
}
2019-05-21 20:31:41 +05:30
2019-05-26 21:50:08 +05:30
// Closing the rename window
function closeRenameWindow(){
//
2019-05-27 10:51:05 +05:30
$('.rename-window').fadeOut('fast')
$("#cover").fadeOut('fast')
2019-05-26 21:50:08 +05:30
}
// Closing the upload window
function closeUploadWindow(){
//
2019-05-27 10:51:05 +05:30
$('.upload-window').fadeOut('fast')
$("#cover").fadeOut('fast')
2019-05-26 21:50:08 +05:30
}
2019-05-21 20:31:41 +05:30
$(document).ready(()=>{
populateContents();
2019-05-24 14:41:30 +05:30
$('.file-download-button').click(()=>{
console.log(currSel)
2019-05-26 21:18:04 +05:30
if(currSel.loc===null){
2019-05-27 10:51:05 +05:30
// This shouldn't happen but ok
2019-05-26 21:18:04 +05:30
alert("Please select a file");
}
else{
$('.file-download-button').after(`<a id="down-temp" href="/files/cat?loc=${currSel.loc}" download="${currSel.name}"></a>`)
document.getElementById('down-temp').click()
$('#down-temp').remove();
}
2019-05-24 14:41:30 +05:30
})
2019-05-24 21:38:31 +05:30
$('.close-rename').click(()=>{
2019-05-27 10:51:05 +05:30
closeRenameWindow();
2019-05-24 21:38:31 +05:30
})
$('.close-upload').click(()=>{
2019-05-27 10:51:05 +05:30
closeUploadWindow();
2019-05-24 21:38:31 +05:30
})
$('.file-rename-button').click(()=>{
2019-05-27 10:51:05 +05:30
if(currSel.loc===nul){
alert("No file selected")
}
else{
$("#nloc-input").val(currSel.loc)
}
2019-05-24 21:38:31 +05:30
$('#cover').fadeIn('fast')
$('.rename-window').fadeIn('fast')
})
$('.nav-bottom-button').click(()=>{
$('#cover').fadeIn('fast')
$('.upload-window').fadeIn('fast')
})
2019-05-26 21:50:08 +05:30
2019-05-27 10:51:05 +05:30
$('.done-rename').click(()=>{
if(currSel.loc===null){
alert("Please select a file");
}
else{
$.ajax("/files/mv",{
method:"post",
data:{
loc:currSel.loc,
nloc:$("#nloc-input").val()
},
success:(msg)=>{
console.log(msg)
populateContents()
alert('Moved')
},
error:msg=>{
console.log(msg)
populateContents()
alert("Could not move")
}
})
}
})
2019-05-27 15:07:36 +05:30
$("#fileInput").change((e)=>{
console.log(e.target.files)
$("#fileLabel").html(e.target.files[0].name);
})
$(".done-upload").click(()=>{
$("#upload-directory").val(currDir.loc)
//$("#theForm").ajaxSubmit({url: '/files/upload', type: 'post'})
$.ajax("/files/upload",{
method: 'post',
processData:false,
contentType:false,
data:new FormData(document.getElementById('upload-form')),
success:msg=>{
alert(msg.done)
closeUploadWindow();
},
error:msg=>alert("Error")
})
})
2019-05-21 20:31:41 +05:30
})