From a89c92e30dbc04214c8eee37cc5fd2a62e3d6772 Mon Sep 17 00:00:00 2001 From: Christopher Rose Date: Thu, 12 Sep 2019 21:25:02 +0530 Subject: [PATCH] Butcher stuff --- static/base.js | 133 ++++++++++++++++++++++++++++--------------------- 1 file changed, 77 insertions(+), 56 deletions(-) diff --git a/static/base.js b/static/base.js index 235beae..0261833 100644 --- a/static/base.js +++ b/static/base.js @@ -1,60 +1,83 @@ - +"use strict"; let currDir = {'loc':'','contents':null} let currSel = {'loc':null,'name':null} -function doUpdate(ele,isDir=false){ - //console.log(ele.attr('data-choice')); - if(ele.hasClass('file-isDir')){ - currDir.loc = ele.attr('data-choice') - populateContents(); - } - if(!isDir){ - currSel.loc = ele.attr('data-choice') - $('.nav-bottom-text').html(currSel.name = ele.html()) - - } + +///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) + } + }) + }) } -function updateContents(contents){ - //console.log(contents) +///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){ + console.log(ele.attr('data-choice')) + postLS(ele.attr('data-choice')).then(e=>{ + if(ele.hasClass('file-isDir')){ + currDir.loc=ele.attr('data-choice') + postLS(currDir.loc); + } + if(!isDir){ + currSel.loc = ele.attr('data-choice') + $('.nav-bottom-text').html( (currSel.name = ele.html()).substring(7) +"..." ) + } + }, + err=>{ + console.log(`E:Something went wrong: ${JSON.stringify(err)}`) + }) +} + +function updateContents(contentResponse){ // 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(contents===null) { + if(contentResponse===null) { $('#files-table').append(`null`) - } - else - { + }else{ $('#files-table').hide().empty(); - contents.contents.forEach(element => { + contentResponse.contents.forEach(element => { $('#files-table').append(`${element.name}`) }); - if(contents.back!=null){ - $('#files-table').prepend(`..`) + if(contentResponse.back!==null){ + $('#files-table').prepend(`..`) } $('#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 function closeRenameWindow(){ - // $('.rename-window').fadeOut('fast') $("#cover").fadeOut('fast') } @@ -67,7 +90,8 @@ function closeUploadWindow(){ $(document).ready(()=>{ - populateContents(); + postLS(".").then(e=>{updateContents(e)},e=>console.log(e)) + //postLS(currDir.loc); $('.file-download-button').click(()=>{ console.log(currSel) if(currSel.loc===null){ @@ -87,7 +111,7 @@ $(document).ready(()=>{ closeUploadWindow(); }) $('.file-rename-button').click(()=>{ - if(currSel.loc===nul){ + if(currSel.loc===null){ alert("No file selected") } else{ @@ -106,25 +130,22 @@ $(document).ready(()=>{ alert("Please select a file"); } else{ - $.ajax("/files/mv",{ - method:"post", - data:{ - loc:currSel.loc, - nloc:$("#nloc-input").val() + postMV(currSel.loc,$("#nloc-input").val()) + .then(response=>{ + postLS(currDir.loc) + }) + .then(response=>{ + alert("Moved") }, - success:(msg)=>{ - console.log(msg) - populateContents() - alert('Moved') - }, - error:msg=>{ - console.log(msg) - populateContents() - alert("Could not move") - } + err=>{ + alert("Not moved") + }) + .catch(err=>{ + console.log("E:Something went wrong, very wrong") }) } }) + $("#fileInput").change((e)=>{ console.log(e.target.files) $("#fileLabel").html(e.target.files[0].name); @@ -140,9 +161,9 @@ $(document).ready(()=>{ success:msg=>{ alert("Uploaded") closeUploadWindow(); - populateContents(); + postLS(currDir.loc); }, error:msg=>alert("Error") }) }) -}) +}) \ No newline at end of file