Butcher stuff

This commit is contained in:
2019-09-12 21:25:02 +05:30
parent 1185904e6c
commit a89c92e30d

View File

@@ -1,60 +1,83 @@
"use strict";
let currDir = {'loc':'','contents':null} let currDir = {'loc':'','contents':null}
let currSel = {'loc':null,'name':null} let currSel = {'loc':null,'name':null}
///Get Path at location
async function postLS(inputPath){
return new Promise((resolve,reject)=>{
$.ajax('/files/ls',{
method:'post',
data:{loc:inputPath},
success:(msg)=>{
resolve(msg);
},
error:(msg)=>{
reject("E:"+msg)
}
})
})
}
///Send request to move/rename files
async function postMV(oldLocation,newLocation){
return new Promise((resolve,reject)=>{
$.ajax({
method:"post",
data:{
"loc":oldLocation,
"nloc":newLocation
},
success:response=>{
resolve(response)
},
error:err=>{
reject(err)
}
})
})
}
function doUpdate(ele,isDir=false){ function doUpdate(ele,isDir=false){
//console.log(ele.attr('data-choice')); console.log(ele.attr('data-choice'))
postLS(ele.attr('data-choice')).then(e=>{
if(ele.hasClass('file-isDir')){ if(ele.hasClass('file-isDir')){
currDir.loc = ele.attr('data-choice') currDir.loc=ele.attr('data-choice')
populateContents(); postLS(currDir.loc);
} }
if(!isDir){ if(!isDir){
currSel.loc = ele.attr('data-choice') currSel.loc = ele.attr('data-choice')
$('.nav-bottom-text').html(currSel.name = ele.html()) $('.nav-bottom-text').html( (currSel.name = ele.html()).substring(7) +"..." )
} }
},
err=>{
console.log(`E:Something went wrong: ${JSON.stringify(err)}`)
})
} }
function updateContents(contents){ function updateContents(contentResponse){
//console.log(contents)
// Change top header contents // Change top header contents
$('#files-location').html(contents.loc) $('#files-location').html(contentResponse.loc)
// if empty, return null, this shouldnt execute if the server is responding properly but ok // if empty, return null, this shouldnt execute if the server is responding properly but ok
if(contents===null) { if(contentResponse===null) {
$('#files-table').append(`<tr><td>null</td><td class="file-handlers"></td></tr>`) $('#files-table').append(`<tr><td>null</td><td class="file-handlers"></td></tr>`)
} }else{
else
{
$('#files-table').hide().empty(); $('#files-table').hide().empty();
contents.contents.forEach(element => { contentResponse.contents.forEach(element => {
$('#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>`) $('#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>`)
}); });
if(contents.back!=null){ if(contentResponse.back!==null){
$('#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>`) $('#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="${contentResponse.back}">..</td></tr>`)
} }
$('#files-table').fadeIn() $('#files-table').fadeIn()
} }
} }
//set table details
function populateContents(){
$.ajax('/files/ls',{
method:'post',
data: currDir,
success:(msg)=>{
updateContents(msg)
}
})
return null;
}
// Closing the rename window // Closing the rename window
function closeRenameWindow(){ function closeRenameWindow(){
//
$('.rename-window').fadeOut('fast') $('.rename-window').fadeOut('fast')
$("#cover").fadeOut('fast') $("#cover").fadeOut('fast')
} }
@@ -67,7 +90,8 @@ function closeUploadWindow(){
$(document).ready(()=>{ $(document).ready(()=>{
populateContents(); postLS(".").then(e=>{updateContents(e)},e=>console.log(e))
//postLS(currDir.loc);
$('.file-download-button').click(()=>{ $('.file-download-button').click(()=>{
console.log(currSel) console.log(currSel)
if(currSel.loc===null){ if(currSel.loc===null){
@@ -87,7 +111,7 @@ $(document).ready(()=>{
closeUploadWindow(); closeUploadWindow();
}) })
$('.file-rename-button').click(()=>{ $('.file-rename-button').click(()=>{
if(currSel.loc===nul){ if(currSel.loc===null){
alert("No file selected") alert("No file selected")
} }
else{ else{
@@ -106,25 +130,22 @@ $(document).ready(()=>{
alert("Please select a file"); alert("Please select a file");
} }
else{ else{
$.ajax("/files/mv",{ postMV(currSel.loc,$("#nloc-input").val())
method:"post", .then(response=>{
data:{ postLS(currDir.loc)
loc:currSel.loc, })
nloc:$("#nloc-input").val() .then(response=>{
alert("Moved")
}, },
success:(msg)=>{ err=>{
console.log(msg) alert("Not moved")
populateContents() })
alert('Moved') .catch(err=>{
}, console.log("E:Something went wrong, very wrong")
error:msg=>{
console.log(msg)
populateContents()
alert("Could not move")
}
}) })
} }
}) })
$("#fileInput").change((e)=>{ $("#fileInput").change((e)=>{
console.log(e.target.files) console.log(e.target.files)
$("#fileLabel").html(e.target.files[0].name); $("#fileLabel").html(e.target.files[0].name);
@@ -140,7 +161,7 @@ $(document).ready(()=>{
success:msg=>{ success:msg=>{
alert("Uploaded") alert("Uploaded")
closeUploadWindow(); closeUploadWindow();
populateContents(); postLS(currDir.loc);
}, },
error:msg=>alert("Error") error:msg=>alert("Error")
}) })