0

I try to create a directory listing with Adobe AIR HTML. Output should be a JSON Document.

As a template I used this node.js code: https://stackoverflow.com/a/11194896/799692

The file walk works. But the JSON object only contain the last Folder.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Adobe Air directory listening to JSON</title>
    <script type="text/javascript" src="AIRAliases.js"></script>  <!-- adobe air framework -->  
    <script type="text/javascript" src="jquery-2.0.3.min.js"></script>
</head>
<body>

<script type="text/javascript">

var completePath = air.File.applicationDirectory.resolvePath("data").nativePath;

window.onload = function()
{   

    air.trace(JSON.stringify(dirTree(completePath), null, 4));

}



function dirTree(filename) 
{

    var currentFile = new air.File(filename);   
    var temp_path = currentFile.nativePath;                 


    info = {
        path: temp_path,
        name: currentFile.name
    };

    if (currentFile.isDirectory)
    {   
        if (currentFile.name !="." && currentFile.name !="..")
            {
            info.type = "folder";
            air.trace(Object.size(info));
            info.children = currentFile.getDirectoryListing().map(function(child) { 
                    return dirTree(child.nativePath);
            });
            }
        }
        else 
        {
            info.type = "file";         

        }   
    return info;                
} //end fn dirTree


// simple helper for object size
 Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};


</script>
</body>
</html>
Community
  • 1
  • 1
oli
  • 95
  • 1
  • 6

1 Answers1

0

Replace the map anonymous function with a function pointer:

function select_child(child) 
  { 
  return dirTree(child.nativePath);
  }

info.children = currentFile.getDirectoryListing().map(select_child);
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265