Make sure expand indicator is correct for trees not loaded yet
[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,8 +27,11 @@
 
 	var collapsed = '[+]';
 	var expanded = '[–]';
+	var indent = '—';
 
-	$('table.treeTable td.expander').text(collapsed);
+	$('a.treeLink').each(function() {
+		$(this).parent().parent().find('td.expander').append(expanderLink($(this).attr('href'), collapsed));
+	});
 
 	$('a.jsTree').live('click', function() {
 		var treeHash = $(this).attr('href').match(/h=([0-9a-fA-F]{40}|HEAD)/);
@@ -36,29 +48,32 @@
 		if (treeRows && treeRows.size() > 0) {
 			if (treeRows.is(':visible')) {
 				treeRows.hide();
-				treeRows.find('td.expander').text(collapsed);
-				row.find('td.expander').text(collapsed);
+				treeRows.each(function() {
+					if ($(this).data('parent') == treeHash) {
+						$(this).data('expanded', false);
+					}
+				});
+				row.find('a.expander').text(collapsed);
 			} else {
-				treeRows.show();
-				treeRows.find('td.expander').each(function() {
-					if ($(this).data('loaded'))
-						$(this).text(expanded);
+				treeRows.each(function() {
+					if (($(this).data('parent') == treeHash) || ($(this).data('expanded') == true)) {
+						$(this).show();
+						$(this).data('expanded', true);
+					}
 				});
-				row.find('td.expander').text(expanded);
+				row.find('a.expander').text(expanded);
 			}
 		} else {
-			var indent = cell.html().match(/^(—+)/);
-			if (indent)
-				indent = indent[1];
-			else
-				indent = '';
-			indent += '—';
+			var depth = row.data('depth');
+			if (depth == null)
+				depth = 0;
+			depth++;
 
 			var img = jQuery(document.createElement('img'));
 			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) {
@@ -73,20 +88,35 @@
 					}
 				});
 
-				subRows.find('td.fileName').prepend(indent);
-				subRows.find('td.expander').text(collapsed);
+				subRows.each(function() {
+
+					$(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)) {
+						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('td.expander').text(expanded);
-				row.find('td.expander').data('loaded', true);
-				cell.children('img.treeSpinner').remove();
+				row.find('a.expander').text(expanded);
+				row.find('img.treeSpinner').remove();
 			});
 		}
 
 		return false;
 	});
-}
+};
 
 $(document).ready(function() {
 	initTree();

comments