Created
July 12, 2016 16:30
-
-
Save Aracki/477fe5246e8c29b6440e49627e30eb0c to your computer and use it in GitHub Desktop.
algorithm for creating file tree structure from flat file paths (for kendo table)
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
// algorithm for creating file tree structure from flat file paths (for kendo table) | |
function createFileTreeStructure(filePaths){ | |
var prefix; | |
var items = { | |
"root" : [] | |
}; | |
if(typeof filePaths === 'undefined' && filePaths.length == 0) { | |
return null; | |
} | |
var pathArray = filePaths[0].split('/'); | |
// base path with orgId/proId/tmp... | |
var basePath = pathArray.splice(0,3).join('/'); | |
for (var i = filePaths.length - 1; i >= 0; i--) { | |
var path = filePaths[i].substring(basePath.length); | |
var index = path.lastIndexOf('/'); | |
var node = { | |
id: filePaths[i], | |
text: path.substring(index + 1), | |
spriteCssClass: 'file' | |
}; | |
if (index <= 0) { | |
items.root.unshift(node); | |
} else { | |
prefix = path.substring(0, index); | |
while ((i>0 && !filePaths[i-1].startsWith(prefix, basePath.length)) || i==0) { | |
index = prefix.lastIndexOf('/'); | |
if (typeof items[prefix] === 'undefined'){ | |
items[prefix] = []; | |
} | |
items[prefix].unshift(node); | |
node = { | |
id: basePath + prefix, | |
text: prefix.substring(index + 1), | |
spriteCssClass: 'folder', | |
items: items[prefix] | |
}; | |
if (index == 0) { | |
prefix = "root"; | |
} else { | |
prefix = prefix.substring(0, index); | |
} | |
} | |
if (typeof items[prefix] === 'undefined'){ | |
items[prefix] = []; | |
} | |
items[prefix].unshift(node); | |
} | |
} | |
var fileTreeData = [{ | |
id: '', | |
text: 'TEST-TRANSFER', | |
expanded: true, | |
spriteCssClass: 'rootfolder', | |
items: items.root | |
}]; | |
return fileTreeData; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sram te bilo, krades od Boke nase