diff --git a/src/webdav.js b/src/webdav.js index 2774d4c..c2cec2b 100644 --- a/src/webdav.js +++ b/src/webdav.js @@ -1,4 +1,5 @@ // A raw WebDAV interface +// callbacks are all : function(body,error,xhrobj). var WebDAV = { GET: function(url, callback) { return this.request('GET', url, {}, null, 'text', callback); @@ -20,6 +21,15 @@ var WebDAV = { return this.request('PUT', url, {}, data, 'text', callback); }, + COPY: function(url, desturl, callback) { + return this.request('COPY',url, {"Destination":desturl, "Depth":'infinity'}, null, 'text', callback); + }, + + MOVE: function(url, desturl, callback) { + return this.request('MOVE',url, {"Destination":desturl, "Depth":'infinity'}, null, 'text', callback); + }, + + request: function(verb, url, headers, data, type, callback) { var xhr = new XMLHttpRequest(); var body = function() { @@ -36,15 +46,12 @@ var WebDAV = { if(callback) { xhr.onreadystatechange = function() { if(xhr.readyState == 4) { // complete. - var b = body(); - if(b) { - callback(b); - } + callback((body() || ""), xhr.status >= 400, xhr); } }; } xhr.open(verb, url, !!callback); - xhr.setRequestHeader("Content-Type", "text/xml; charset=UTF-8"); + if(data != null) xhr.setRequestHeader("Content-Type", "text/xml; charset=UTF-8"); for (var header in headers) { xhr.setRequestHeader(header, headers[header]); } @@ -58,13 +65,36 @@ var WebDAV = { // An Object-oriented API around WebDAV. WebDAV.Fs = function(rootUrl) { - this.rootUrl = rootUrl; + //Make url absolute + if(!/^http/.test(rootUrl)) rootUrl = location.protocol + '//' + location.host + rootUrl; + + this.rootUrl = rootUrl.replace(/\/$/, ''); // Strip trailing slash; + var fs = this; - this.file = function(href) { + function addcommon(obj) { + //Copy: callback will receive the new file/dir object or undefined if error occurs with xhr object to fetch details + obj.copy = function(desturl, callback) { + return WebDAV.COPY(this.url, fs.urlFor(desturl), function(body,error,xhr) { + callback(error ? undefined : fs[obj.type](desturl),xhr); + }); + } + + obj.move = function(desturl, callback) { + return WebDAV.MOVE(this.url, fs.urlFor(desturl), function(body,error,xhr) { + callback(error ? undefined : fs[obj.type](desturl),xhr); + }); + } + + this.rm = function(callback) { + return WebDAV.DELETE(this.url, callback); + } + } + + this.file = function(href, urlisabsolute) { this.type = 'file'; - this.url = fs.urlFor(href); + this.url = urlisabsolute ? href : fs.urlFor(href);; this.name = fs.nameFor(this.url); @@ -76,17 +106,15 @@ WebDAV.Fs = function(rootUrl) { return WebDAV.PUT(this.url, data, callback); }; - this.rm = function(callback) { - return WebDAV.DELETE(this.url, callback); - }; - + addcommon(this); + return this; }; - this.dir = function(href) { + this.dir = function(href,urlisabsolute) { this.type = 'dir'; - this.url = fs.urlFor(href); + this.url = urlisabsolute ? href : fs.urlFor(href); this.name = fs.nameFor(this.url); @@ -97,19 +125,19 @@ WebDAV.Fs = function(rootUrl) { } var result = []; // Start at 1, because the 0th is the same as self. - for(var i=1; i< doc.childNodes.length; i++) { - var response = doc.childNodes[i]; - var href = response.getElementsByTagName('D:href')[0].firstChild.nodeValue; + for(var i=1; i< doc.children.length; i++) { + var response = doc.children[i]; + var href = response.getElementsByTagNameNS('DAV:','href')[0].firstChild.nodeValue; href = href.replace(/\/$/, ''); // Strip trailing slash - var propstat = response.getElementsByTagName('D:propstat')[0]; - var prop = propstat.getElementsByTagName('D:prop')[0]; - var resourcetype = prop.getElementsByTagName('D:resourcetype')[0]; - var collection = resourcetype.getElementsByTagName('D:collection')[0]; + var propstat = response.getElementsByTagNameNS('DAV:','propstat')[0]; + var prop = propstat.getElementsByTagNameNS('DAV:','prop')[0]; + var resourcetype = prop.getElementsByTagNameNS('DAV:','resourcetype')[0]; + var collection = resourcetype.getElementsByTagNameNS('DAV:','collection')[0]; if(collection) { - result[i-1] = new fs.dir(href); + result[i-1] = new fs.dir(href,true); } else { - result[i-1] = new fs.file(href); + result[i-1] = new fs.file(href,true); } } return result; @@ -124,9 +152,7 @@ WebDAV.Fs = function(rootUrl) { } }; - this.rm = function(callback) { - return WebDAV.DELETE(this.url, callback); - }; + addcommon(this); this.mkdir = function(callback) { return WebDAV.MKCOL(this.url, callback);