I have a Django admin page for a nested category list like this:
I wrote this script to sort the list and present it hierarchically:
{% extends "admin/change_list.html" %}
{% load i18n %}
{% block footer %}
<script>
(function(){
var rows=document.getElementById('result_list').getElementsByTagName('tr'),
table=rows[1].parentNode||rows[1].parentElement,
i=0, r, // skip the first row
data={}; // store category data
while (r=rows[++i]) {
var catName=r.getElementsByTagName('a')[0],
k=catName.innerHTML,
opts=r.getElementsByTagName('select')[0],
j=-1, opt;
while (opt=opts[++j]) {
if (!opt.selected) continue;
data[k] = {
title: k,
children: {},
parentName: opt.innerHTML,
parentId: opt.value,
catName: catName,
row: r
}
}
}
for (var sub in data) {
if (data[sub].parentName == sub) continue;
for (var sup in data) {
if (sup == data[sub].parentName) {
data[sup].children[sub]=data[sub];
data[sub].parent = data[sup];
break;
}
}
}
var alt = 0;
for (var leaf in data) {
if (data[leaf].parentName != leaf) continue;
walk(data[leaf], leaf, function (node, nodeName) {
var n=node, t=n.title;
while (n=n.parent) {
t = ' · ' + t;
}
node.catName.innerHTML = t;
node.row['class']=node.row['className']='row'+alt++%2;
table.removeChild(node.row);
table.appendChild(node.row);
});
}
function walk (leaf, leafName, cb) {
if (cb) cb(leaf, leafName);
leaf.ready = true;
for (var kid in leaf.children) {
if (leaf.children[kid].ready) continue;
walk(leaf.children[kid], kid, cb);
}
}
}());
</script>
{% endblock %}
...the script runs fine and the list looks like this:
My question is: I feel like the script is prone to memory leaks in UAs with weak garbage collection because of the circular references created by the parent / child stuff. Is this something I should be worried about? Is there a better way to write the script? Should I be deleting a bunch of stuff at the end of the script, and if so, what?