Fix all the path joining nastiness by writing a smart helper armadillo.App.joinPath().
authorRobert Sesek <rsesek@bluestatic.org>
Mon, 4 Oct 2010 13:19:26 +0000 (09:19 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Mon, 4 Oct 2010 13:19:26 +0000 (09:19 -0400)
web_frontend/main.js
web_frontend/path_control.js

index df72ec37ef5747e20a8a54f0b72e82a3becd2ca2..52201438275d99b5c5a0b105b5b0807ccc6d80f5 100644 (file)
@@ -138,6 +138,24 @@ armadillo.App.prototype.stripLastPathComponent = function(path) {
   return '/';
 };
 
+/**
+ * Joins all the arguments together as a path.
+ * @param  {string...}  varargs  Components to join
+ */
+armadillo.App.prototype.joinPath = function() {
+  var path = '';
+  var sep = '/';
+  var last = arguments.length - 1;
+  goog.array.forEach(arguments, function (c, i) {
+    if (c == sep && i != 0)
+      return;
+    path += c;
+    if (c[c.length - 1] != sep && i != last)
+      path += sep;
+  });
+  return path;
+};
+
 /**
  * Clears the error message.
  */
index dd863bc62be36a160647bcba4c3e0ece15f78b60..9d00f0e0cd528bda7299eeacac46a463f401d173 100644 (file)
@@ -95,15 +95,9 @@ armadillo.PathControl.prototype.decorateInternal = function(element) {
   this.element_ = element;
   var components = this.path_.split('/');
 
-  if (components.length == 2) {
-    // If this is an item that lives at the root, generate a special node for
-    // moving between items at the top level.
-    components[0] = '/';
-  } else {
-    // Otherwise, just remove it as the first node will list all items at the
-    // root.
-    goog.array.removeAt(components, 0);
-  }
+  // If this is an item that lives at the root, generate a special node for
+  // moving between items at the top level.
+  components[0] = '/';
 
   // If the last component is emtpy, do not use it because it means a directory
   // is being moved.
@@ -111,10 +105,10 @@ armadillo.PathControl.prototype.decorateInternal = function(element) {
     goog.array.removeAt(components, components.length - 1);
   }
 
-  var path = '/';
+  var path = '';
   goog.array.forEach(components, function (part, i) {
     this.addChild(this.createComponentNode_(path, part), true);
-    path += part + '/';
+    path = app.joinPath(path, part);
   }, this);
 
   if (this.editableLastComponent_) {
@@ -174,14 +168,14 @@ armadillo.PathControl.prototype.fetchMenuContents_ = function(path, name, menu)
         return;
       }
       var item = new goog.ui.MenuItem(caption);
-      item.setValue(path + caption);
+      item.setValue(app.joinPath(path, name, caption));
       menu.addItem(item);
       if (caption == name) {
         menu.setHighlighted(item);
       }
     });
   };
-  app.sendRequest('list', {'path':path}, callback);
+  app.sendRequest('list', {'path' : app.joinPath(path, name)}, callback);
 };
 
 /**