What I wish to achieve is the following:
- Use some backend logic to generate a JSON which represents a directory structure.
- pass on this JSON to a JavaScript function to create the HTML that represents the directory structure.
The HTML I'd like to generate is the following:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
</head>
<body>
<h1>Contents of "Some"</h1>
<ul>
<li data-toggle="collapse" data-target="#first">Collapsible </li>
<ul id="first" class="collapse">
<li>Lorem ipsum dolor text....</li>
<li>Lorem ipsum dolor text....</li>
<li>Lorem ipsum dolor text....</li>
<li data-toggle="collapse" data-target="#sub">Collapsible</li>
<ul id="sub" class="collapse">
<li>Lorem ipsum dolor text....</li>
<li>Lorem ipsum dolor text....</li>
<li>Lorem ipsum dolor text....</li>
</ul>
</ul>
<li> File 1</li>
<li> File 2</li>
<li> File 3</li>
</ul>
</body>
</html>
To generate this, I am assuming my backend program can generate a JSON as follows:
<script>
var tree = [
{'name': 'file1',
'type': 'file',
'url': 'https://www.google.com'
},
{'name': 'file2',
'type': 'file',
'url': 'https://www.google.com'
},
{'name': 'file1',
'type': 'file',
'url': 'https://www.google.com'
},
{'name': 'directory1',
'type': 'folder',
'url': 'https://www.google.com',
'contents': [
{'name': 'file1',
'type': 'file',
'url': 'https://www.google.com'
},
{'name': 'file2',
'type': 'file',
'url': 'https://www.google.com'
},
]
},
]
The URL in above case will be replaced by some custom URL which will enable a person to download the file. This does not matter for the problem at hand.
Now, to generate the final HTML code, I have been trying to use recursion in Javascript to construct the DOM structure. But for some reason I am unable to figure out, I end up constructing an infinite DOM. This is my current attempt.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
</head>
<body>
<script>
var tree = [{
'name': 'file1',
'type': 'file',
'url': 'https://www.google.com'
},
{
'name': 'file2',
'type': 'file',
'url': 'https://www.google.com'
},
{
'name': 'file1',
'type': 'file',
'url': 'https://www.google.com'
},
{
'name': 'directory1',
'type': 'folder',
'url': 'https://www.google.com',
'contents': [{
'name': 'file1',
'type': 'file',
'url': 'https://www.google.com'
},
{
'name': 'file2',
'type': 'file',
'url': 'https://www.google.com'
},
]
},
]
</script>
<h1>Contents of "Some"</h1>
<div id="tree_container">
</div>
<script>
var listContentsOf = function(tree, container = 'tree_container', depth = '1') {
console.log(tree);
console.log(container);
console.log(depth);
$('#' + container).append('<ul id="' + depth + '"> </ul>');
for (i = 0; i < tree.length; i++) {
if (tree[i].type == 'file') {
$('#' + depth).append('<li>' + tree[i].name + '</li>');
} else if (tree[i].type == 'folder') {
$('#' + depth).append('<li>' + tree[i].name + '</li>');
subID = depth + i;
$('#' + depth).append('<div id="' + subID + 'holder"> </div>');
console.log(tree[i].contents);
console.log(subID + 'holder');
// listContentsOf(tree[i].contents, subID + 'holder', subID);
}
}
};
listContentsOf(tree);
</script>
</body>
<
The last recursive call in the 'if else' is commented because it goes into an infinite run. Any ideas on why the infinite execution happens and how to circumvent it, would be greatly appreciated.