Refactor treeview javascript helper