Store depth as data
[gitphp.git] / js / tree.js
blob:a/js/tree.js -> blob:b/js/tree.js
--- a/js/tree.js
+++ b/js/tree.js
@@ -9,6 +9,15 @@
  * @subpackage Javascript
  */
 
+function expanderLink(href, text) {
+	var a = jQuery(document.createElement('a'));
+	a.attr('href', href);
+	a.text(text);
+	a.addClass('jsTree');
+	a.addClass('expander');
+	return a;
+};
+
 function initTree() {
 	var url = window.location.href.match(/^([^\?]+\/)/);
 	if (!url) {
@@ -18,14 +27,10 @@
 
 	var collapsed = '[+]';
 	var expanded = '[–]';
+	var indent = '—';
 
-	$('a.jsTree').each(function() {
-		var a = jQuery(document.createElement('a'));
-		a.attr('href', $(this).attr('href'));
-		a.text(collapsed);
-		a.addClass('jsTree');
-		a.addClass('expander');
-		$(this).parent().parent().find('td.expander').append(a);
+	$('a.treeLink').each(function() {
+		$(this).parent().parent().find('td.expander').append(expanderLink($(this).attr('href'), collapsed));
 	});
 
 	$('a.jsTree').live('click', function() {
@@ -44,8 +49,9 @@
 			if (treeRows.is(':visible')) {
 				treeRows.hide();
 				treeRows.each(function() {
-					if ($(this).data('parent') == treeHash)
+					if ($(this).data('parent') == treeHash) {
 						$(this).data('expanded', false);
+					}
 				});
 				row.find('a.expander').text(collapsed);
 			} else {
@@ -67,19 +73,13 @@
 			img.attr('src', url + "images/tree-loader.gif");
 			img.attr('alt', GITPHP_RES_LOADING);
 			img.addClass('treeSpinner');
-			img.appendTo(cell);
+			row.find('a.treeLink').after(img);
 
 			$.get($(this).attr('href'), { o: 'js' },
 			function(data) {
 				var subRows = jQuery(data);
 
 				subRows.addClass(treeHash);
-
-				subRows.each(function() {
-					$(this).data('parent', treeHash);
-					$(this).data('expanded', true);
-					$(this).data('depth', depth);
-				});
 
 				var classList = row.attr('class').split(/\s+/);
 				$.each(classList, function(index, item) {
@@ -88,32 +88,35 @@
 					}
 				});
 
-				var fileCell = subRows.find('td.fileName');
-				for (var i = 0; i < depth; i++) {
-					fileCell.prepend('—');
-				}
 				subRows.each(function() {
-					var treeLink = $(this).find('a.jsTree');
+
+					$(this).data('parent', treeHash);
+					$(this).data('expanded', true);
+					$(this).data('depth', depth);
+
+					var fileCell = $(this).find('td.fileName');
+					var treeLink = $(this).find('a.treeLink');
 					if (treeLink && (treeLink.size() > 0)) {
-						var a1 = jQuery(document.createElement('a'));
-						a1.attr('href', treeLink.attr('href'));
-						a1.text(collapsed);
-						a1.addClass('jsTree');
-						a1.addClass('expander');
-						$(this).find('td.expander').append(a1);
+						fileCell.prepend(expanderLink(treeLink.attr('href'), collapsed));
+					} else {
+						fileCell.prepend(indent);
+					}
+
+					for (var i = 1; i < depth; i++) {
+						fileCell.prepend(indent);
 					}
 				});
 
 				row.after(subRows);
 
 				row.find('a.expander').text(expanded);
-				cell.children('img.treeSpinner').remove();
+				row.find('img.treeSpinner').remove();
 			});
 		}
 
 		return false;
 	});
-}
+};
 
 $(document).ready(function() {
 	initTree();

comments