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: Switched to featherlight from fancybox, organised private functions to make them easier to find, prefixed all private variables with _ for easy distinction, moved pretty print from iframe eval to properly using prettify
Updates to add in user messaging, fix listing bug for some WebDAV servers, use server mimeType and audio preview
Date: 1 year ago
Size: 11,030 bytes
 

Contents

Class file image Download
!function(e){"sendAsBinary"in XMLHttpRequest.prototype||(XMLHttpRequest.prototype.sendAsBinary=function(e){function t(e){return 255&e.charCodeAt(0)}var n=Array.prototype.map.call(e,t),r=new Uint8Array(n)
this.send(r.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 t=function(){var t,n=function(t){return t.directory?t.item.find(".title").on("click",function(){return history.pushState(history.state,t.path+t.name,t.path+t.name),L.list(t.path+t.name),!1}):t.item.find(".title").on("click",function(n){if(n.stopPropagation(),"video"==t.type)e.featherlight('<video autoplay controls><source src="'+t.path+t.name+'"/></video>'),n.preventDefault()
else if("audio"==t.type)e.featherlight('<audio autoplay controls><source src="'+t.path+t.name+'"/></audio>'),n.preventDefault()
else if("image"==t.type)e.featherlight({image:t.path+t.name}),n.preventDefault()
else if("font"==t.type){var r={eot:"embedded-opentype",otf:"opentype",ttf:"truetype"},i=t.name.replace(/^.+\.([^\.]+)$/,"$1"),o=(t.path+t.name).replace(/\W+/g,"_"),s="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"
e('[data-path="'+(t.path+t.name)+'"]').is("style")||e("body").appendChild('<style type="text/css" data-path="'+(t.path+t.name)+'">@font-face{font-family:"'+o+'";src:url("'+t.path+t.name+'") format("'+(r[i]||i)+'")}</style>'),e.featherlight('<h1 style="font-family:"'+o+'">'+t.name+"</h1><p style=\"font-family:'"+o+"';font-size:1.5em\">"+s+"</p><p style=\"font-family:'"+o+"'\">"+a+"</p><p style=\"font-family:'"+o+"'\"><strong>"+s+"</strong></p><p style=\"font-family:'"+o+"'\"><em>"+s+'</em></p><p><a href="'+t.path+t.name+'" style="display:inline-block;padding:.5em;background:#000;font-family:sans-serif;border-radius:.5em;color:#fff">Download</a></p>'),n.preventDefault()}else"text"==t.type&&("code"in e.featherlight.contentFilters||e.extend(e.featherlight.contentFilters,{code:{process:function(t){var n=e.Deferred(),r=e('<pre class="prettyprint"></pre>')
return e.ajax(t,{complete:function(e,t){"error"!==t&&(r.text(e.responseText),n.resolve(r),PR.prettyPrint()),n.fail()}}),n.promise()}}}),e.featherlight({code:t.path+t.name}),n.preventDefault())}),t["delete"]&&(t.item.find(".delete").on("click",function(){return confirm('Are you sure you want to delete "'+t.name+'"?')&&L.del(t),!1}),t.item.find(".rename").on("click",function(){var e=prompt('Please enter the new name for "'+t.name+'":',t.name)
return e.match(/^[a-z0-9_\-\.]+$/i)||(f("Bad file name."),e=!1),e&&L.rename(t,t.path+e),!1}),t.item.find(".copy").on("click",function(){return f("Currently not implemented."),!1}),t.item.find(".move").on("click",function(){return f("Currently not implemented."),!1})),t.item.on("click",function(){return t.item.find("a.title").click(),!1}),t.item},r=function(t){var n=!1
return e.each(E,function(){return this.name==t.name?(n=this,!1):void 0}),n},i=function(t){return t.item=e("<li/>").data("file",t),t.directory?t.item.addClass("directory"):(t.item.addClass("file"),t.type?t.item.addClass(t.type):t.item.addClass("unknown")),t.directory||t.item.addClass(t.name.replace(/^.+\.([^\.]+)$/,"$1")),t.item.append('<a href="'+t.path+t.name+'" target="_blank" class="title">'+t.title+"</a>"),t.directory||t.item.append('<span class="size">'+h(t.size)+"</span>"),t.name&&(t["delete"]&&(t.item.append('<a href="#delete" title="Delete" class="delete">&times;</a>'),t.item.append('<a href="#move" title="Move" class="move">move</a>')),t.item.append('<a href="#rename" title="Rename" class="rename">rename</a>'),t.item.append('<a href="#copy" title="Copy" class="copy">copy</a>')),n(t),t},o=function(e){return e=e.replace(/\/$/,""),e.split("/").pop()},s=function(e,t){return e.querySelector?e.querySelector(t):e.getElementsByTagName(t)[0]},l=function(e,t){var n=s(e,t)
return n?n.textContent:""},d=function(e,t){return e.querySelectorAll?Array.from(e.querySelectorAll(t)):Array.from(e.getElementsByTagName(t))},c=function(t){if(t.mimeType&&t.mimeType.split("/").shift())return t.mimeType.split("/").shift()
var n={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},r="unknown"
return e.each(n,function(e,n){return t.match(n)?(r=e,!1):void 0}),r},u=function(e,t){var n=y("PROPFIND",e,{Depth:1})
return Object.keys(t).forEach(function(e){n.addEventListener(e,t[e],!0)}),n.send(null),n},f=function(t,n){"notify"in e?e.notify(t,{className:n||"error"}):console.log(t)},p=function(){return L.list(x)},m=function(){return v(),b.empty(),e.each(E,function(e,t){t&&b.append(t.item)}),b},y=function(e,t,n,r){var a=new XMLHttpRequest
return r||(t+=(t.indexOf("?")>-1?"&":"?")+"_="+Date.now()),a.addEventListener("loadstart",function(){q=!0}),a.addEventListener("loadend",function(){q=!0}),a.open(e,t,!0),n&&Object.keys(n).forEach(function(e){a.setRequestHeader(e,n[e])}),a},h=function(e){var t=""
return["bytes","KB","MB","GB","TB","PB"].forEach(function(n,r){!t&&e<Math.pow(1024,r+1)&&(t+=(e/Math.pow(1024,r)).toFixed(r>0?1:0)+" "+(1==e?"byte":n))}),t},v=function(){return E.length&&E.sort(function(e,t){return e.directory==t.directory?e.name.replace(/\/$/,"")<t.name.replace(/\/$/,"")?-1:1:e.directory?-1:1}),e.each(E,function(e){this.index=e}),E},g=function(){v(),m()},q=!1,w={},E=[],b=e('<ul class="list"/>'),x=window.location.pathname,L={init:function(){e('<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(e("body").empty()),e("div.content").append(b),t=e("div.upload"),L.list(x),m(),t.on("dragover",function(){return t.addClass("active"),!1}),t.on("dragend dragleave",function(e){return t.removeClass("active"),!1}),t.on("drop",function(n){var a=n.originalEvent.target.files||n.originalEvent.dataTransfer.files
return t.removeClass("active"),e.each(a,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 E[existingFile.index]}if("undefined"!=typeof FileReader){var n=new FileReader
n.addEventListener("load",function(e){t.data=e.target.result,L.upload(t)},!1),n.context=L,n.filename=t.name,n.readAsBinaryString(t)}else f("Sorry, your browser isn't currently suppored.")}),!1}),e("a.create-directory").on("click",function(){var e,t=prompt("New folder name:")
if(!t.match(/^[\w\d_\-\.]+$/))return alert("Name contains non-standard characters, aborting."),!1
if(t.match(/^\.\.?$/))return alert("Cannot use a reserved name for your directory."),!1
if(e=r(t))return e.directory?alert('Directory "'+e.name+'" already exists.'):alert('A file called "'+e.name+'" exists, unable to create folder.'),!1
var e={directory:!0,name:t,title:t+"/",path:x,modified:Date.now(),size:!1,type:c(t),mimeType:"",request:null,item:null,data:null,"delete":!0}
return e.request=y("MKCOL",e.path+e.name),e.request.addEventListener("loadstart",function(t){e.item.addClass("loading")},!1),e.request.addEventListener("load",function(t){e.item.removeClass("loading")},!1),e.request.addEventListener("error",function(t){delete E[e.index],g(),f("Error creating directory "+e.name+".")},!1),e.request.addEventListener("abort",function(t){delete E[e.index],g(),f("Aborted as requested.","success")},!1),E.push(i(e)),g(),e.request.send(null),!1}),e(window).on("popstate",function(e){L.list(window.location.pathname)}),e(document).on("keydown",function(e){var t=e.which||e.keyCode
return 116==t||82==t&&(e.metaKey||e.ctrlKey)?(e.preventDefault(),L.list(x,!0),!1):!0})},list:function(t,n){return t=t.match(/\/$/)?t:t+"/",t in w&&!n?(E=[],w[x=t].forEach(function(e){E.push(i(e))}),g()):void u(t,{loadstart:function(){e("div.content").addClass("loading")},loadend:function(){e("div.content").removeClass("loading")},load:function(e){var n=e.target,r=new DOMParser,a=r.parseFromString(n.responseText,"application/xml")
x=t,E=[],d(a,"response").forEach(function(e,n){var r=l(e,"href"),a=o(r)
return n?void E.push(i({directory:!!s(e,"collection"),name:a,title:a,path:x,modified:new Date(l(e,"getlastmodified")),size:l(e,"getcontentlength"),type:c(a),mimeType:l(e,"getcontenttype"),request:null,item:null,data:null,"delete":!0})):void("/"!=t&&E.push(i({directory:!0,name:"",title:"&larr;",path:t.replace(/[^\/]+\/?$/,""),modified:"",size:"",type:"",mimeType:"",request:null,item:null,data:null,"delete":!1})))}),E.timestamp=Date.now(),w[x]=E,g()},error:function(){f("There was an error getting details for "+t+".")},abort:function(){f("Aborted as requested. "+t,"success")}})},upload:function(t){return t.name?(t=e.extend({directory:!1,title:t.name,path:x,modified:new Date,size:t.data.length,request:null,item:null,"delete":!0},t),t.request=y("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){p()},!1),t.request.addEventListener("error",function(e){delete E[t.index],g(),f("Error uploading file.")},!1),t.request.addEventListener("abort",function(e){delete E[t.index],g(),f("Aborted as requested.","sucess")},!1),E.push(i(t)),g(),t.request.sendAsBinary(t.data),!0):!1},del:function(e){return e.name?("path"in e||(e.path=x),e.request=y("DELETE",e.path+e.name),e.request.addEventListener("load",function(t){delete E[e.index],p()},!1),e.request.addEventListener("error",function(t){f("Error deleting file "+e.name+".")},!1),e.request.addEventListener("abort",function(e){f("Aborted as requested.","success")},!1),e.request.send(null),!0):!1},copy:function(e,t){return e.request=y("COPY",e.path+e.name,{Destination:t}),e.request.addEventListener("load",function(e){p()},!1),e.request.addEventListener("error",function(e){f("Error copying file "+file.name+".")},!1),e.request.addEventListener("abort",function(e){f("Aborted as requested.","success")},!1),e.request.send(null),!0},move:function(e,t){return e.request=y("MOVE",e.path+e.name,{Destination:window.location.protocol+"//"+window.location.host+t}),e.request.addEventListener("load",function(e){p()},!1),e.request.addEventListener("error",function(e){f("Error moving file "+file.name+".")},!1),e.request.addEventListener("abort",function(e){f("Aborted as requested.","success")},!1),e.request.send(null),!0},rename:function(e,t){return this.move(e,t)}}
return L}()
e(function(){t.init()})}(jQuery)