Commit 388d72e6bf9c3e72113b8c16d1cc266b41870eb9

Authored by Robert Speicher
1 parent a8fad4ff

Add render_tree helper; simplify (speed up) tree_icon

app/assets/javascripts/tree.js.coffee
@@ -15,6 +15,7 @@ $ -> @@ -15,6 +15,7 @@ $ ->
15 $("#tree-slider .tree-item").live 'click', (e) -> 15 $("#tree-slider .tree-item").live 'click', (e) ->
16 $('.tree-item-file-name a', this).trigger('click') if (e.target.nodeName != "A") 16 $('.tree-item-file-name a', this).trigger('click') if (e.target.nodeName != "A")
17 17
  18 + # Show/Hide the loading spinner
18 $('#tree-slider .tree-item-file-name a, .breadcrumb a, .project-refs-form').live 19 $('#tree-slider .tree-item-file-name a, .breadcrumb a, .project-refs-form').live
19 "ajax:beforeSend": -> $('.tree_progress').addClass("loading") 20 "ajax:beforeSend": -> $('.tree_progress').addClass("loading")
20 "ajax:complete": -> $('.tree_progress').removeClass("loading") 21 "ajax:complete": -> $('.tree_progress').removeClass("loading")
app/decorators/tree_decorator.rb
@@ -8,14 +8,14 @@ class TreeDecorator < ApplicationDecorator @@ -8,14 +8,14 @@ class TreeDecorator < ApplicationDecorator
8 8
9 #parts = parts[0...-1] if is_blob? 9 #parts = parts[0...-1] if is_blob?
10 10
11 - yield(h.link_to("..", "#", remote: :true)) if parts.count > max_links 11 + yield(h.link_to("..", "#", remote: true)) if parts.count > max_links
12 12
13 parts.each do |part| 13 parts.each do |part|
14 part_path = File.join(part_path, part) unless part_path.empty? 14 part_path = File.join(part_path, part) unless part_path.empty?
15 part_path = part if part_path.empty? 15 part_path = part if part_path.empty?
16 16
17 next unless parts.last(2).include?(part) if parts.count > max_links 17 next unless parts.last(2).include?(part) if parts.count > max_links
18 - yield(h.link_to(h.truncate(part, length: 40), h.project_tree_path(project, h.tree_join(ref, part_path)), remote: :true)) 18 + yield(h.link_to(h.truncate(part, length: 40), h.project_tree_path(project, h.tree_join(ref, part_path)), remote: true))
19 end 19 end
20 end 20 end
21 end 21 end
app/helpers/tree_helper.rb
1 module TreeHelper 1 module TreeHelper
2 - def tree_icon(content)  
3 - if content.is_a?(Grit::Blob)  
4 - if content.text?  
5 - image_tag "file_txt.png"  
6 - elsif content.image?  
7 - image_tag "file_img.png" 2 + # Sorts a repository's tree so that folders are before files and renders
  3 + # their corresponding partials
  4 + #
  5 + # contents - A Grit::Tree object for the current tree
  6 + def render_tree(contents)
  7 + # Render Folders before Files/Submodules
  8 + folders, files = contents.partition { |v| v.kind_of?(Grit::Tree) }
  9 +
  10 + tree = ""
  11 +
  12 + # Render folders if we have any
  13 + tree += render partial: 'tree/tree_item', collection: folders, locals: {type: 'folder'} if folders.present?
  14 +
  15 + files.each do |f|
  16 + if f.respond_to?(:url)
  17 + # Object is a Submodule
  18 + tree += render partial: 'tree/submodule_item', object: f
8 else 19 else
9 - image_tag "file_bin.png" 20 + # Object is a Blob
  21 + tree += render partial: 'tree/tree_item', object: f, locals: {type: 'file'}
10 end 22 end
11 - else  
12 - image_tag "file_dir.png"  
13 end 23 end
  24 +
  25 + tree.html_safe
14 end 26 end
15 27
16 - def tree_hex_class(content)  
17 - "file_#{hexdigest(content.name)}" 28 + # Return an image icon depending on the file type
  29 + #
  30 + # type - String type of the tree item; either 'folder' or 'file'
  31 + def tree_icon(type)
  32 + image = type == 'folder' ? 'file_dir.png' : 'file_txt.png'
  33 + image_tag(image, size: '16x16')
18 end 34 end
19 35
20 - def tree_full_path(content)  
21 - content.name.force_encoding('utf-8')  
22 - if params[:path]  
23 - File.join(params[:path], content.name)  
24 - else  
25 - content.name  
26 - end 36 + def tree_hex_class(content)
  37 + "file_#{hexdigest(content.name)}"
27 end 38 end
28 39
29 # Public: Determines if a given filename is compatible with GitHub::Markup. 40 # Public: Determines if a given filename is compatible with GitHub::Markup.
app/views/tree/_tree.html.haml
@@ -14,7 +14,6 @@ @@ -14,7 +14,6 @@
14 - if tree.is_blob? 14 - if tree.is_blob?
15 = render partial: "tree/tree_file", object: tree 15 = render partial: "tree/tree_file", object: tree
16 - else 16 - else
17 - - contents = tree.contents  
18 %table#tree-slider{class: "table_#{@hex_path} tree-table" } 17 %table#tree-slider{class: "table_#{@hex_path} tree-table" }
19 %thead 18 %thead
20 %th Name 19 %th Name
@@ -24,22 +23,16 @@ @@ -24,22 +23,16 @@
24 = link_to "History", tree.history_path, class: "right" 23 = link_to "History", tree.history_path, class: "right"
25 24
26 - if tree.up_dir? 25 - if tree.up_dir?
27 - %tr{ class: "tree-item", url: tree.up_dir_path } 26 + %tr.tree-item
28 %td.tree-item-file-name 27 %td.tree-item-file-name
29 - = image_tag "file_empty.png"  
30 - = link_to "..", tree.up_dir_path, remote: :true 28 + = image_tag "file_empty.png", size: '16x16'
  29 + = link_to "..", tree.up_dir_path, remote: true
31 %td 30 %td
32 %td 31 %td
33 32
34 - - index = 0  
35 - - contents.select{ |i| i.is_a?(Grit::Tree)}.each do |content|  
36 - = render partial: "tree/tree_item", locals: { content: content, index: (index += 1) }  
37 - - contents.select{ |i| i.is_a?(Grit::Blob)}.each do |content|  
38 - = render partial: "tree/tree_item", locals: { content: content, index: (index += 1) }  
39 - - contents.select{ |i| i.is_a?(Grit::Submodule)}.each do |content|  
40 - = render partial: "tree/submodule_item", locals: { content: content, index: (index += 1) } 33 + = render_tree(tree.contents)
41 34
42 - - if content = contents.select{ |c| c.is_a?(Grit::Blob) and c.name =~ /^readme/i }.first 35 + - if content = tree.contents.find { |c| c.is_a?(Grit::Blob) and c.name =~ /^readme/i }
43 .file_holder#README 36 .file_holder#README
44 .file_title 37 .file_title
45 %i.icon-file 38 %i.icon-file
app/views/tree/_tree_item.html.haml
1 -- file = tree_full_path(content)  
2 -%tr{ class: "tree-item #{tree_hex_class(content)}", url: project_tree_path(@project, tree_join(@id, file)) } 1 +%tr{ class: "tree-item #{tree_hex_class(tree_item)}" }
3 %td.tree-item-file-name 2 %td.tree-item-file-name
4 - = tree_icon(content)  
5 - %strong= link_to truncate(content.name, length: 40), project_tree_path(@project, tree_join(@id || @commit.id, file)), remote: :true 3 + = tree_icon(type)
  4 + %strong= link_to truncate(tree_item.name, length: 40), project_tree_path(@project, tree_join(@id || @commit.id, tree_item.name)), remote: true
6 %td.tree_time_ago.cgray 5 %td.tree_time_ago.cgray
7 - - if index == 1  
8 - %span.log_loading  
9 - Loading commit data..  
10 - = image_tag "ajax_loader_tree.gif", width: 14 6 + %span.log_loading.hide
  7 + Loading commit data...
  8 + = image_tag "ajax_loader_tree.gif", width: 14
11 %td.tree_commit 9 %td.tree_commit
app/views/tree/show.js.haml
1 :plain 1 :plain
2 // Load Files list 2 // Load Files list
3 - $("#tree-holder").html("#{escape_javascript(render(partial: "tree", locals: {commit: @commit, tree: @tree}))}"); 3 + $("#tree-holder").html("#{escape_javascript(render(partial: "tree", locals: {tree: @tree}))}");
4 $("#tree-content-holder").show("slide", { direction: "right" }, 150); 4 $("#tree-content-holder").show("slide", { direction: "right" }, 150);
5 $('.project-refs-form #path').val("#{@path}"); 5 $('.project-refs-form #path').val("#{@path}");
6 6