Permalink
Browse files

Added a simple browser example

  • Loading branch information...
1 parent f2a5c48 commit a4d4183402e2ca601659628fa0b24f9547c7280c @aslakhellesoy committed Mar 12, 2009
Showing with 106 additions and 19 deletions.
  1. +12 −2 README.textile
  2. +42 −0 example/index.html
  3. +19 −0 example/jquery-1.3.2.min.js
  4. +7 −5 spec/webdav_spec.js
  5. +26 −12 src/webdav.js
View
@@ -67,9 +67,19 @@ h3. Dir.rm(callback)
Removes the directory.
-h2. Usage
+h2. Usage/Example
-Not written yet - haven't used it in a browser yet :-)
+Check out example/index.html
+
+To see it in action in a browser, stand in the root folder and run:
+
+<pre>
+ruby spec/webdav_server.rb
+</pre>
+
+Then point your browser to http://localhost:10080/example/index.html
+
+You should be able to browse and edit everything from within your browser.
h2. Running tests
View
@@ -0,0 +1,42 @@
+<html>
+ <head>
+ <script type="text/javascript" src="../src/webdav.js"></script>
+ <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
+ <script type="text/javascript">
+ var fs = new WebDAV.Fs('http://localhost:10080');
+
+ var urlid = function(u) {
+ return u.replace(/:/g, '_').replace(/\//g, '_');
+ };
+
+ var loadDir = function(dir, parentNode) {
+ dir.children(function(children) {
+ var ulId = urlid(dir.url) + '_ul';
+
+ var ul = '<ul id="' + ulId + '">'
+ for (var c in children) {
+ ul += '<li class="' + children[c].type + '" id="' +
+ urlid(children[c].url) + '"><a href="' +
+ children[c].url + '">' + children[c].name + '</a></li>';
+ }
+ ul += '</ul>';
+
+ $(parentNode).append(ul);
+ $('#' + ulId).children('.dir').click(function(e) {
+ loadDir(fs.dir(e.target.href), '#' + urlid(e.target.href));
+ return false;
+ });
+ });
+ }
+
+ $(document).ready(function(){
+ loadDir(fs.dir('/'), '#root')
+ });
+ </script>
+ </head>
+ <body>
+ <h1>WebDAV.js example</h1>
+
+ <div id="root"></div>
+ </body>
+</html>
View
@@ -24,11 +24,13 @@ suite('WebDAV.Fs')
var fs = new WebDAV.Fs('http://localhost:10080');
var dir = fs.dir('/a');
- assertEquals(3, dir.children().length);
+ assertEquals(2, dir.children().length);
- assertEquals('http://localhost:10080/a', dir.children()[0].url);
- assertEquals('http://localhost:10080/a/b/', dir.children()[1].url);
- assertEquals('http://localhost:10080/a/bla', dir.children()[2].url);
+ assertEquals('b', dir.children()[0].name);
+ assertEquals('bla', dir.children()[1].name);
+
+ assertEquals('http://localhost:10080/a/b', dir.children()[0].url);
+ assertEquals('http://localhost:10080/a/bla', dir.children()[1].url);
})
test('should mkdir', function() {
@@ -46,7 +48,7 @@ suite('WebDAV.Fs')
dir.mkdir();
}
assertTrue(expectedFailure);
- assertEquals(1, dir.children().length);
+ assertEquals(0, dir.children().length);
})
xtest('should create file', function() {
View
@@ -33,24 +33,24 @@ var WebDAV = {
return b;
};
- var async = !(callback == null);
- if(async) {
- xhr.onreadystatechange = function(readyState) {
- if(readyState == 4 || readyState == null) { // complete. (null check is workaround for env.rhino.js).
- if(body()) { // Workaround for env.rhino.js
- callback(body());
+ if(callback) {
+ xhr.onreadystatechange = function() {
+ if(xhr.readyState == 4) { // complete.
+ var b = body();
+ if(b) {
+ callback(b);
}
}
};
}
- xhr.open(verb, url, async);
+ xhr.open(verb, url, !!callback);
xhr.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
for (var header in headers) {
xhr.setRequestHeader(header, headers[header]);
}
xhr.send(data);
- if(!async) {
+ if(!callback) {
return body();
}
}
@@ -62,8 +62,12 @@ WebDAV.Fs = function(rootUrl) {
var fs = this;
this.file = function(href) {
+ this.type = 'file';
+
this.url = fs.urlFor(href);
-
+
+ this.name = fs.nameFor(this.url);
+
this.read = function(callback) {
return WebDAV.GET(this.url, callback);
};
@@ -80,26 +84,32 @@ WebDAV.Fs = function(rootUrl) {
};
this.dir = function(href) {
+ this.type = 'dir';
+
this.url = fs.urlFor(href);
+ this.name = fs.nameFor(this.url);
+
this.children = function(callback) {
var childrenFunc = function(doc) {
if(doc.childNodes == null) {
throw('No such directory: ' + url);
}
var result = [];
- for(var i=0; i< doc.childNodes.length; i++) {
+ // 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;
+ 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];
if(collection) {
- result[i] = new fs.dir(href);
+ result[i-1] = new fs.dir(href);
} else {
- result[i] = new fs.file(href);
+ result[i-1] = new fs.file(href);
}
}
return result;
@@ -128,6 +138,10 @@ WebDAV.Fs = function(rootUrl) {
this.urlFor = function(href) {
return (/^http/.test(href) ? href : this.rootUrl + href);
};
+
+ this.nameFor = function(url) {
+ return url.replace(/.*\/(.*)/, '$1');
+ };
return this;
};

0 comments on commit a4d4183

Please sign in to comment.