2

所以我正在尝试使用 github api 并将其转换为用于 dynatree。

我正在寻找使用javascript。但如果在 Ruby、ClojureScript 等中更容易完成,那也可以。

所以 Github api 返回的东西看起来像:

 {
 "sha": "a1f28431d5ee1a00361ff7e1e2dc91172d5bbabd",
 "url": "https://api.github.com/repos/Jonovono/c/git/trees/a1f28431d5ee1a00361ff7e1e2dc91172d5bbabd",
"tree": [
{
  "mode": "100644",
  "type": "blob",
  "sha": "b81f704ee24f08f5f3e00675a9912da79274c3bc",
  "path": ".gitignore",
  "size": 114,
  "url":  "https://api.github.com/repos/Jonovono/c/git/blobs/b81f704ee24f08f5f3e00675a9912da79274c3bc"
  },
   {
  "mode": "100644",
  "type": "blob",
  "sha": "c6231c2f161f50bededaaac624a7a96db6179e9a",
  "path": "Makefile.am",
  "size": 40,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/c6231c2f161f50bededaaac624a7a96db6179e9a"
   },
  {
  "mode": "100644",
  "type": "blob",
  "sha": "ca41e5b8fb710b4ae7a2cdb35cdbafb143382dc2",
  "path": "README.md",
  "size": 2194,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/ca41e5b8fb710b4ae7a2cdb35cdbafb143382dc2"
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "dd3b02c5fcf3312ef7a83f4b6370a8b546739bc1",
  "path": "config.h.in",
  "size": 625,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/dd3b02c5fcf3312ef7a83f4b6370a8b546739bc1"
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "e242df0dc8be1fec7b2da2091ab8161aaee1b7a2",
  "path": "configure.ac",
  "size": 156,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/e242df0dc8be1fec7b2da2091ab8161aaee1b7a2"
},
{
  "mode": "100755",
  "type": "blob",
  "sha": "df8eea7e4ce8862105fcd7929b20bdb45488048b",
  "path": "depcomp",
  "size": 18615,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/df8eea7e4ce8862105fcd7929b20bdb45488048b"
},
{
  "mode": "100755",
  "type": "blob",
  "sha": "6781b987bdbcbc23efe6bbe1654a1e3637b9af07",
  "path": "install-sh",
  "size": 13663,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/6781b987bdbcbc23efe6bbe1654a1e3637b9af07"
},
{
  "mode": "100755",
  "type": "blob",
  "sha": "28055d2ae6f2a2c584afcd769d7881e11f62ecd9",
  "path": "missing",
  "size": 11419,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/28055d2ae6f2a2c584afcd769d7881e11f62ecd9"
},
{
  "mode": "040000",
  "type": "tree",
  "sha": "57bb4a62d2de582c74b2e6de71e3024db7905cac",
  "path": "src",
  "url": "https://api.github.com/repos/Jonovono/c/git/trees/57bb4a62d2de582c74b2e6de71e3024db7905cac"
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "daaed524883bd510502834d472e9be6cff4e3a69",
  "path": "src/Makefile.am",
  "size": 59,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/daaed524883bd510502834d472e9be6cff4e3a69"
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "cf3f8f549fe870315eb4417c9f84dec587c8286b",
  "path": "src/c.c",
  "size": 9480,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/cf3f8f549fe870315eb4417c9f84dec587c8286b"
}
 ]
}

所以基本上我想获取数组中的所有路径,如“Makefile.am”、“src”(识别出这是一个文件夹)、“src/cc”。这也适用于递归文件夹。

dynatree 的格式如下:

  var obj = [
    { title: 'Lazy node 1', isLazy: true },
    { title: 'Lazy node 2', isLazy: true },
    { title: 'Folder node 3', isFolder: true,
      children: [
        { title: 'node 3.1' },
        { title: 'node 3.2',
          children: [
            { title: 'node 3.2.1' },
            { title: 'node 3.2.2',
              children: [
                { title: 'node 3.2.2.1' }
              ]
            }
          ]
        }
      ]
    }
  ];

或者

  var rootNode = $("#tree").dynatree("getRoot");
  var childNode = rootNode.addChild({
    title: "Programatically addded nodes",
    tooltip: "This folder and all child nodes were added programmatically.",
    isFolder: true
  });
  childNode.addChild({
    title: "Document using a custom icon",
    icon: "customdoc1.gif"
  });

我不确定哪一个更容易到达。我在想我可以使用某种类型的哈希表,但我只是不确定如何让它递归地为文件工作,比如:“src/a/b/c/index.text”

4

1 回答 1

7

这是一个简单的解决方案,它采用类似字符串"/src/hello/world"并将其路径添加到对象

files = {};
function parseFolder(path){
    var cur = files;
    path.split("/").slice(1).forEach(function(elem){
        cur[elem] = cur[elem] || {}; 
        cur = cur[elem];
    });
}

用法类似于

parseFolder("/src/stuff/hello");
parseFolder("/src/stuff/world");
parseFolder("/bin/stuff/world");

这是一个工作的jsfiddle

这不是 Dynatree 识别的格式,但您明白了,您应该将对象推送到数组中,而不是添加命名为路径的属性。

于 2013-05-23T03:15:40.100 回答