Icontem

File: src/webdav-min.js

Recommend this page to a friend!
  Classes of Dom Hastings  >  JS Webdav Client  >  src/webdav-min.js  >  Download  
File: src/webdav-min.js
Role: Class source
Content type: text/plain
Description: Class source
Class: JS Webdav Client
Access files of a Webdav server
Author: By
Last change: Fix issue #4
Date: 2 months ago
Size: 10,881 bytes
 

Contents

Class file image Download
!function(o){"sendAsBinary"in XMLHttpRequest.prototype||(XMLHttpRequest.prototype.sendAsBinary=function(e){var t=Array.prototype.map.call(e,function(e){return 255&e.charCodeAt(0)}),n=new Uint8Array(t);this.send(n.buffer)}),"from"in Array||(Array.from=function(e){return[].slice.call(e)}),"keys"in Object||(Object.keys=function(e){var t=[];for(var n in e)e.hasOwnProperty(n)&&t.push(n);return t});var n,r,s,l,d,c,u,i,e,f,t,p,m,y,h,v,g,q,w=(r=function(e){var t=!1;return o.each(h,function(){if(this.name==e.name)return t=this,!1}),t},s=function(e){var i;return e.item=o("<li/>").data("file",e),e.directory?e.item.addClass("directory"):(e.item.addClass("file"),e.type?e.item.addClass(e.type):e.item.addClass("unknown")),e.directory||e.item.addClass(e.name.replace(/^.+\.([^\.]+)$/,"$1")),e.item.append('<a href="'+e.path+e.name+'" target="_blank" class="title">'+e.title+"</a>"),e.directory||e.item.append('<span class="size">'+t(e.size)+"</span>"),e.name&&(e.delete&&(e.item.append('<a href="#delete" title="Delete" class="delete">&times;</a>'),e.item.append('<a href="#move" title="Move" class="move">move</a>')),e.item.append('<a href="#rename" title="Rename" class="rename">rename</a>'),e.item.append('<a href="#copy" title="Copy" class="copy">copy</a>')),(i=e).directory?i.item.find(".title").on("click",function(){return history.pushState(history.state,i.path+i.name,i.path+i.name),q.list(i.path+i.name),!1}):i.item.find(".title").on("click",function(e){if(e.stopPropagation(),"video"==i.type)o.featherlight('<video autoplay controls><source src="'+i.path+i.name+'"/></video>'),e.preventDefault();else if("audio"==i.type)o.featherlight('<audio autoplay controls><source src="'+i.path+i.name+'"/></audio>'),e.preventDefault();else if("image"==i.type)o.featherlight({image:i.path+i.name}),e.preventDefault();else if("font"==i.type){var t=i.name.replace(/^.+\.([^\.]+)$/,"$1"),n=(i.path+i.name).replace(/\W+/g,"_"),r="The quick brown fox jumps over the lazy dog. 0123456789<br/>Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz";o('[data-path="'+(i.path+i.name)+'"]').is("style")||o("body").appendChild('<style type="text/css" data-path="'+(i.path+i.name)+'">@font-face{font-family:"'+n+'";src:url("'+i.path+i.name+'") format("'+({eot:"embedded-opentype",otf:"opentype",ttf:"truetype"}[t]||t)+'")}</style>'),o.featherlight('<h1 style="font-family:"'+n+'">'+i.name+"</h1><p style=\"font-family:'"+n+"';font-size:1.5em\">"+r+"</p><p style=\"font-family:'"+n+"'\">"+a+"</p><p style=\"font-family:'"+n+"'\"><strong>"+r+"</strong></p><p style=\"font-family:'"+n+"'\"><em>"+r+'</em></p><p><a href="'+i.path+i.name+'" style="display:inline-block;padding:.5em;background:#000;font-family:sans-serif;border-radius:.5em;color:#fff">Download</a></p>'),e.preventDefault()}else"text"==i.type&&("code"in o.featherlight.contentFilters||o.extend(o.featherlight.contentFilters,{code:{process:function(e){var n=o.Deferred(),r=o('<pre class="prettyprint"></pre>');return o.ajax(e,{complete:function(e,t){"error"!==t&&(r.text(e.responseText),n.resolve(r),PR.prettyPrint()),n.fail()}}),n.promise()}}}),o.featherlight({code:i.path+i.name}),e.preventDefault())}),i.delete&&(i.item.find(".delete").on("click",function(){return confirm('Are you sure you want to delete "'+i.name+'"?')&&q.del(i),!1}),i.item.find(".rename").on("click",function(){var e=prompt('Please enter the new name for "'+i.name+'":',i.name);return e.match(/^[a-z0-9_\-\.]+$/i)||(u("Bad file name."),e=!1),e&&q.rename(i,i.path+e),!1}),i.item.find(".copy").on("click",function(){return u("Currently not implemented."),!1}),i.item.find(".move").on("click",function(){return u("Currently not implemented."),!1})),i.item.on("click",function(){return i.item.find("a.title").click(),!1}),i.item,e},l=function(e,t){return e.querySelector?e.querySelector(t):e.getElementsByTagName(t)[0]},d=function(e,t){var n=l(e,t);return n?n.textContent:""},c=function(n){if(n.mimeType&&n.mimeType.split("/").shift())return n.mimeType.split("/").shift();var r="unknown";return o.each({text:/\.(?:te?xt|i?nfo|php|pl|cgi|faq|ini|htaccess|log|md|sql|sfv|conf|sh|pl|pm|py|rb|(?:s?c|sa)ss|js|java|coffee|[sx]?html?|xml)$/i,image:/\.(?:jpe?g|gif|a?png|svg)/i,video:/\.(?:mp(?:e?g)?4|mov|avi|webm|ogv)/i,audio:/\.(?:mp3|wav|ogg)/i,font:/\.(?:woff2?|eot|[ot]tf)/i},function(e,t){if(n.match(t))return r=e,!1}),r},u=function(e,t){"notify"in o?o.notify(e,{className:t||"error"}):console.log(e)},i=function(){return q.list(g)},e=function(){return p(),v.empty(),o.each(h,function(e,t){t&&v.append(t.item)}),v},f=function(e,t,n,r){var a=new XMLHttpRequest;return r||(t+=(-1<t.indexOf("?")?"&":"?")+"_="+Date.now()),a.addEventListener("loadstart",function(){!0}),a.addEventListener("loadend",function(){!0}),a.open(e,t,!0),n&&Object.keys(n).forEach(function(e){a.setRequestHeader(e,n[e])}),a},t=function(n){var r="";return["bytes","KB","MB","GB","TB","PB"].forEach(function(e,t){!r&&n<Math.pow(1024,t+1)&&(r+=(n/Math.pow(1024,t)).toFixed(0<t?1:0)+" "+(1==n?"byte":e))}),r},p=function(){return h.length&&h.sort(function(e,t){return e.directory==t.directory?e.name.replace(/\/$/,"")<t.name.replace(/\/$/,"")?-1:1:e.directory?-1:1}),o.each(h,function(e){this.index=e}),h},m=function(){p(),e()},y={},h=[],v=o('<ul class="list"/>'),g=window.location.pathname,q={init:function(){o('<div class="content"></div><div class="upload">Drop files here to upload or <a href="#createDirectory" class="create-directory">create a new directory</a></div>').appendTo(o("body").empty()),o("div.content").append(v),n=o("div.upload"),q.list(g),e(),n.on("dragover",function(){return n.addClass("active"),!1}),n.on("dragend dragleave",function(e){return n.removeClass("active"),!1}),n.on("drop",function(e){var t=e.originalEvent.target.files||e.originalEvent.dataTransfer.files;return n.removeClass("active"),o.each(t,function(e,t){if(existingFile=r(t)){if(!confirm('A file called "'+existingFile.name+'" already exists, would you like to overwrite it?'))return!1;delete h[existingFile.index]}if("undefined"!=typeof FileReader){var n=new FileReader;n.addEventListener("load",function(e){t.data=e.target.result,q.upload(t)},!1),n.context=q,n.filename=t.name,n.readAsBinaryString(t)}else u("Sorry, your browser isn't currently suppored.")}),!1}),o("a.create-directory").on("click",function(){var t,e=prompt("New folder name:");return e.match(/^[\w\d_\-\.]+$/)?e.match(/^\.\.?$/)?alert("Cannot use a reserved name for your directory."):(t=r(e))?t.directory?alert('Directory "'+t.name+'" already exists.'):alert('A file called "'+t.name+'" exists, unable to create folder.'):((t={directory:!0,name:e,title:e+"/",path:g,modified:Date.now(),size:!1,type:c(e),mimeType:"",request:null,item:null,data:null,delete:!0}).request=f("MKCOL",t.path+t.name),t.request.addEventListener("loadstart",function(e){t.item.addClass("loading")},!1),t.request.addEventListener("load",function(e){t.item.removeClass("loading")},!1),t.request.addEventListener("error",function(e){delete h[t.index],m(),u("Error creating directory "+t.name+".")},!1),t.request.addEventListener("abort",function(e){delete h[t.index],m(),u("Aborted as requested.","success")},!1),h.push(s(t)),m(),t.request.send(null)):alert("Name contains non-standard characters, aborting."),!1}),o(window).on("popstate",function(e){q.list(window.location.pathname)}),o(document).on("keydown",function(e){var t=e.which||e.keyCode;return 116!=t&&(82!=t||!e.metaKey&&!e.ctrlKey)||(e.preventDefault(),q.list(g,!0),!1)})},list:function(i,e){if((i=i.match(/\/$/)?i:i+"/")in y&&!e)return h=[],y[g=i].forEach(function(e){h.push(s(e))}),m();var t,n;t={loadstart:function(){o("div.content").addClass("loading")},loadend:function(){o("div.content").removeClass("loading")},load:function(e){var t,n,r=e.target,a=(new DOMParser).parseFromString(r.responseText,"application/xml");g=i,h=[],(t=a,n="response",t.querySelectorAll?Array.from(t.querySelectorAll(n)):Array.from(t.getElementsByTagName(n))).forEach(function(e,t){var n=d(e,"href"),r=n.replace(/\/$/,"").split("/").pop();t?h.push(s({directory:!!l(e,"collection"),name:r,title:decodeURIComponent(r),path:g,modified:new Date(d(e,"getlastmodified")),size:d(e,"getcontentlength"),type:c(r),mimeType:d(e,"getcontenttype"),request:null,item:null,data:null,delete:!0})):"/"!=i&&h.push(s({directory:!0,name:"",title:"&larr;",path:i.replace(/[^\/]+\/?$/,""),modified:"",size:"",type:"",mimeType:"",request:null,item:null,data:null,delete:!1}))}),h.timestamp=Date.now(),y[g]=h,m()},error:function(){u("There was an error getting details for "+i+".")},abort:function(){u("Aborted as requested. "+i,"success")}},n=f("PROPFIND",i,{Depth:1}),Object.keys(t).forEach(function(e){n.addEventListener(e,t[e],!0)}),n.send(null)},upload:function(t){return!!t.name&&((t=o.extend({directory:!1,title:decodeURIComponent(t.name),path:g,modified:new Date,size:t.data.length,request:null,item:null,delete:!0},t)).request=f("PUT",t.path+t.name,{"Content-Type":t.type}),t.request.addEventListener("loadstart",function(e){t.item.addClass("loading"),t.item.find("span.size").after('<span class="uploading"><span class="progress"><span class="meter"></span></span><span class="cancel-upload">&times;</span></span>'),t.item.find("span.cancel-upload").on("click",function(){return t.request.abort(),!1})},!1),t.request.addEventListener("progress",function(e){t.item.find("span.meter").width(e.position/e.total*100+"%")},!1),t.request.addEventListener("load",function(e){i()},!1),t.request.addEventListener("error",function(e){delete h[t.index],m(),u("Error uploading file.")},!1),t.request.addEventListener("abort",function(e){delete h[t.index],m(),u("Aborted as requested.","sucess")},!1),h.push(s(t)),m(),t.request.sendAsBinary(t.data),!0)},del:function(t){return!!t.name&&("path"in t||(t.path=g),t.request=f("DELETE",t.path+t.name),t.request.addEventListener("load",function(e){delete h[t.index],y[g]=h,i()},!1),t.request.addEventListener("error",function(e){u("Error deleting file "+t.name+".")},!1),t.request.addEventListener("abort",function(e){u("Aborted as requested.","success")},!1),t.request.send(null),!0)},copy:function(e,t){return e.request=f("COPY",e.path+e.name,{Destination:t}),e.request.addEventListener("load",function(e){i()},!1),e.request.addEventListener("error",function(e){u("Error copying file "+file.name+".")},!1),e.request.addEventListener("abort",function(e){u("Aborted as requested.","success")},!1),e.request.send(null),!0},move:function(e,t){return e.request=f("MOVE",e.path+e.name,{Destination:window.location.protocol+"//"+window.location.host+t}),e.request.addEventListener("load",function(e){i()},!1),e.request.addEventListener("error",function(e){u("Error moving file "+file.name+".")},!1),e.request.addEventListener("abort",function(e){u("Aborted as requested.","success")},!1),e.request.send(null),!0},rename:function(e,t){return this.move(e,t)}});o(function(){w.init()})}(jQuery);