treedrilldown.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * GitPHP javascript tree
  3. *
  4. * Load subtree data into tree page asynchronously
  5. *
  6. * @author Christopher Han <xiphux@gmail.com>
  7. * @copyright Copyright (c) 2011 Christopher Han
  8. * @package GitPHP
  9. * @subpackage Javascript
  10. */
  11. define(["jquery", "modules/geturl", 'modules/resources'],
  12. function($, url, resources) {
  13. var collapsed = '[+]';
  14. var expanded = '[–]';
  15. var indent = '—';
  16. var treeTable = null;
  17. function expanderLink(href, text) {
  18. var a = $(document.createElement('a'));
  19. a.attr('href', href);
  20. a.text(text);
  21. a.addClass('jsTree');
  22. a.addClass('expander');
  23. return a;
  24. }
  25. function createExpanders() {
  26. treeTable.find('a.treeLink').each(function() {
  27. var jThis = $(this);
  28. var href = jThis.attr('href');
  29. jThis.parent().parent().find('td.expander').append(expanderLink(href, collapsed));
  30. });
  31. }
  32. function toggleTreeRows(treeHash, parentRow, rows) {
  33. if (rows.is(':visible')) {
  34. rows.hide();
  35. rows.each(function() {
  36. var jThis = $(this);
  37. if (jThis.data('parent') == treeHash) {
  38. jThis.data('expanded', false);
  39. }
  40. });
  41. parentRow.find('a.expander').text(collapsed);
  42. } else {
  43. rows.each(function() {
  44. var jThis = $(this);
  45. if ((jThis.data('parent') == treeHash) || (jThis.data('expanded') == true)) {
  46. jThis.show();
  47. jThis.data('expanded', true);
  48. }
  49. });
  50. parentRow.find('a.expander').text(expanded);
  51. }
  52. }
  53. function loadTreeRows(treeHash, parentRow, href) {
  54. var depth = parentRow.data('depth') || 0;
  55. depth++;
  56. var img = $(document.createElement('img'));
  57. img.attr('src', url + "images/tree-loader.gif");
  58. img.attr('alt', resources.Loading);
  59. img.addClass('treeSpinner');
  60. parentRow.find('a.treeLink').after(img);
  61. $.get(href, { o: 'js' },
  62. function(data) {
  63. var subRows = $(data);
  64. subRows.addClass(treeHash);
  65. var classList = parentRow.attr('class').split(/\s+/);
  66. $.each(classList, function(index, item) {
  67. if (item.match(/[0-9a-fA-F]{40}/)) {
  68. subRows.addClass(item);
  69. }
  70. });
  71. subRows.each(function() {
  72. var jThis = $(this);
  73. jThis.data('parent', treeHash);
  74. jThis.data('expanded', true);
  75. jThis.data('depth', depth);
  76. var fileCell = jThis.find('td.fileName');
  77. var treeLink = jThis.find('a.treeLink');
  78. if (treeLink && (treeLink.size() > 0)) {
  79. fileCell.prepend(expanderLink(treeLink.attr('href'), collapsed));
  80. } else {
  81. fileCell.prepend(indent);
  82. }
  83. for (var i = 1; i < depth; i++) {
  84. fileCell.prepend(indent);
  85. }
  86. });
  87. parentRow.after(subRows);
  88. parentRow.find('a.expander').text(expanded);
  89. parentRow.find('img.treeSpinner').remove();
  90. });
  91. }
  92. var expanderClick = function() {
  93. var jThis = $(this);
  94. var treeHash = jThis.attr('href').match(/h=([0-9a-fA-F]{4,40}|HEAD)/);
  95. if (!treeHash) {
  96. treeHash = jThis.attr('href').match(/\/trees\/([0-9a-fA-F]{4,40})/);
  97. }
  98. if (!treeHash) {
  99. return false;
  100. }
  101. treeHash = treeHash[1];
  102. var cell = $(this).parent();
  103. var row = cell.parent();
  104. var treeRows = treeTable.find('.' + treeHash);
  105. if (treeRows && (treeRows.size() > 0)) {
  106. toggleTreeRows(treeHash, row, treeRows);
  107. } else {
  108. loadTreeRows(treeHash, row, jThis.attr('href'));
  109. }
  110. return false;
  111. }
  112. var init = function(treeTableElem) {
  113. treeTable = treeTableElem;
  114. createExpanders();
  115. treeTable.find('a.jsTree').live('click', expanderClick);
  116. };
  117. return {
  118. init: init
  119. };
  120. }
  121. );