Last active
December 17, 2015 16:19
-
-
Save Jonovono/5637574 to your computer and use it in GitHub Desktop.
Create Github.com API response to use for dynatree.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var und = require("underscore"); | |
var parsedFile = require("./file.json"); | |
var tree = parsedFile.tree | |
files = []; | |
function parseFolder(name, path, type) { | |
var paths = und.map(tree, function(item) {return {path: item.path, | |
type: item.type}}); | |
setupRoot(paths); | |
var cur = files; | |
var split = path.split("/"); | |
if (split.length === 1) { | |
return; | |
} | |
var previous = split[0]; | |
split.forEach(function(elem){ | |
var el = und.findWhere(cur, {title: elem}); | |
if (el) { | |
console.log("exists") | |
} else { | |
// This will be a bigger loop | |
if (type === "blob" && elem === name) { | |
var prev = und.findWhere(cur, {title: previous}); | |
cur.push({title: name}); | |
} else { | |
cur.push({title: elem, children: []}); | |
} | |
} | |
var nextLoop = und.findWhere(cur, {title: elem}); | |
cur = nextLoop.children; | |
previous = elem; | |
}); | |
} | |
function loadFile() { | |
for (var i in tree) { | |
obj = tree[i]; | |
var type = obj.type; | |
var path = obj.path; | |
var name = path.replace(/^.*[\\\/]/, '') | |
parseFolder(name, path, type); | |
} | |
} | |
function setupRoot(paths) { | |
paths.forEach(function(path) { | |
var type = path.type; | |
var path = path.path; | |
var split = path.split("/"); | |
var root = split[0]; | |
if (type === "blob" && !und.findWhere(files, {title: root})) { | |
files.push({title: root}); | |
} else if (type === "tree" && !und.findWhere(files, {title: root, isFolder: true})) { | |
files.push({title: root, isFolder: true, children: []}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment