Skip to content

Instantly share code, notes, and snippets.

@Aracki
Created July 12, 2016 16:30
Show Gist options
  • Save Aracki/477fe5246e8c29b6440e49627e30eb0c to your computer and use it in GitHub Desktop.
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)
// 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;
}
@ogi1992
Copy link

ogi1992 commented Dec 19, 2016

Sram te bilo, krades od Boke nase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment