From 388d72e6bf9c3e72113b8c16d1cc266b41870eb9 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Wed, 3 Oct 2012 18:40:56 -0400 Subject: [PATCH] Add render_tree helper; simplify (speed up) tree_icon --- app/assets/javascripts/tree.js.coffee | 1 + app/decorators/tree_decorator.rb | 4 +-- app/helpers/tree_helper.rb | 47 +++++++++++++++++---------- app/views/tree/_tree.html.haml | 17 +++------- app/views/tree/_tree_item.html.haml | 14 ++++---- app/views/tree/show.js.haml | 2 +- 6 files changed, 44 insertions(+), 41 deletions(-) diff --git a/app/assets/javascripts/tree.js.coffee b/app/assets/javascripts/tree.js.coffee index 200f1e06..83cc08dc 100644 --- a/app/assets/javascripts/tree.js.coffee +++ b/app/assets/javascripts/tree.js.coffee @@ -15,6 +15,7 @@ $ -> $("#tree-slider .tree-item").live 'click', (e) -> $('.tree-item-file-name a', this).trigger('click') if (e.target.nodeName != "A") + # Show/Hide the loading spinner $('#tree-slider .tree-item-file-name a, .breadcrumb a, .project-refs-form').live "ajax:beforeSend": -> $('.tree_progress').addClass("loading") "ajax:complete": -> $('.tree_progress').removeClass("loading") diff --git a/app/decorators/tree_decorator.rb b/app/decorators/tree_decorator.rb index 38236c2c..754868f5 100644 --- a/app/decorators/tree_decorator.rb +++ b/app/decorators/tree_decorator.rb @@ -8,14 +8,14 @@ class TreeDecorator < ApplicationDecorator #parts = parts[0...-1] if is_blob? - yield(h.link_to("..", "#", remote: :true)) if parts.count > max_links + yield(h.link_to("..", "#", remote: true)) if parts.count > max_links parts.each do |part| part_path = File.join(part_path, part) unless part_path.empty? part_path = part if part_path.empty? next unless parts.last(2).include?(part) if parts.count > max_links - yield(h.link_to(h.truncate(part, length: 40), h.project_tree_path(project, h.tree_join(ref, part_path)), remote: :true)) + yield(h.link_to(h.truncate(part, length: 40), h.project_tree_path(project, h.tree_join(ref, part_path)), remote: true)) end end end diff --git a/app/helpers/tree_helper.rb b/app/helpers/tree_helper.rb index 81a16989..65c23799 100644 --- a/app/helpers/tree_helper.rb +++ b/app/helpers/tree_helper.rb @@ -1,31 +1,42 @@ module TreeHelper - def tree_icon(content) - if content.is_a?(Grit::Blob) - if content.text? - image_tag "file_txt.png" - elsif content.image? - image_tag "file_img.png" + # Sorts a repository's tree so that folders are before files and renders + # their corresponding partials + # + # contents - A Grit::Tree object for the current tree + def render_tree(contents) + # Render Folders before Files/Submodules + folders, files = contents.partition { |v| v.kind_of?(Grit::Tree) } + + tree = "" + + # Render folders if we have any + tree += render partial: 'tree/tree_item', collection: folders, locals: {type: 'folder'} if folders.present? + + files.each do |f| + if f.respond_to?(:url) + # Object is a Submodule + tree += render partial: 'tree/submodule_item', object: f else - image_tag "file_bin.png" + # Object is a Blob + tree += render partial: 'tree/tree_item', object: f, locals: {type: 'file'} end - else - image_tag "file_dir.png" end + + tree.html_safe + end + + # Return an image icon depending on the file type + # + # type - String type of the tree item; either 'folder' or 'file' + def tree_icon(type) + image = type == 'folder' ? 'file_dir.png' : 'file_txt.png' + image_tag(image, size: '16x16') end def tree_hex_class(content) "file_#{hexdigest(content.name)}" end - def tree_full_path(content) - content.name.force_encoding('utf-8') - if params[:path] - File.join(params[:path], content.name) - else - content.name - end - end - # Public: Determines if a given filename is compatible with GitHub::Markup. # # filename - Filename string to check diff --git a/app/views/tree/_tree.html.haml b/app/views/tree/_tree.html.haml index c25907fb..920a70b5 100644 --- a/app/views/tree/_tree.html.haml +++ b/app/views/tree/_tree.html.haml @@ -14,7 +14,6 @@ - if tree.is_blob? = render partial: "tree/tree_file", object: tree - else - - contents = tree.contents %table#tree-slider{class: "table_#{@hex_path} tree-table" } %thead %th Name @@ -24,22 +23,16 @@ = link_to "History", tree.history_path, class: "right" - if tree.up_dir? - %tr{ class: "tree-item", url: tree.up_dir_path } + %tr.tree-item %td.tree-item-file-name - = image_tag "file_empty.png" - = link_to "..", tree.up_dir_path, remote: :true + = image_tag "file_empty.png", size: '16x16' + = link_to "..", tree.up_dir_path, remote: true %td %td - - index = 0 - - contents.select{ |i| i.is_a?(Grit::Tree)}.each do |content| - = render partial: "tree/tree_item", locals: { content: content, index: (index += 1) } - - contents.select{ |i| i.is_a?(Grit::Blob)}.each do |content| - = render partial: "tree/tree_item", locals: { content: content, index: (index += 1) } - - contents.select{ |i| i.is_a?(Grit::Submodule)}.each do |content| - = render partial: "tree/submodule_item", locals: { content: content, index: (index += 1) } + = render_tree(tree.contents) - - if content = contents.select{ |c| c.is_a?(Grit::Blob) and c.name =~ /^readme/i }.first + - if content = tree.contents.find { |c| c.is_a?(Grit::Blob) and c.name =~ /^readme/i } .file_holder#README .file_title %i.icon-file diff --git a/app/views/tree/_tree_item.html.haml b/app/views/tree/_tree_item.html.haml index 226c380f..87311e19 100644 --- a/app/views/tree/_tree_item.html.haml +++ b/app/views/tree/_tree_item.html.haml @@ -1,11 +1,9 @@ -- file = tree_full_path(content) -%tr{ class: "tree-item #{tree_hex_class(content)}", url: project_tree_path(@project, tree_join(@id, file)) } +%tr{ class: "tree-item #{tree_hex_class(tree_item)}" } %td.tree-item-file-name - = tree_icon(content) - %strong= link_to truncate(content.name, length: 40), project_tree_path(@project, tree_join(@id || @commit.id, file)), remote: :true + = tree_icon(type) + %strong= link_to truncate(tree_item.name, length: 40), project_tree_path(@project, tree_join(@id || @commit.id, tree_item.name)), remote: true %td.tree_time_ago.cgray - - if index == 1 - %span.log_loading - Loading commit data.. - = image_tag "ajax_loader_tree.gif", width: 14 + %span.log_loading.hide + Loading commit data... + = image_tag "ajax_loader_tree.gif", width: 14 %td.tree_commit diff --git a/app/views/tree/show.js.haml b/app/views/tree/show.js.haml index 174e3e03..fadd5e22 100644 --- a/app/views/tree/show.js.haml +++ b/app/views/tree/show.js.haml @@ -1,6 +1,6 @@ :plain // Load Files list - $("#tree-holder").html("#{escape_javascript(render(partial: "tree", locals: {commit: @commit, tree: @tree}))}"); + $("#tree-holder").html("#{escape_javascript(render(partial: "tree", locals: {tree: @tree}))}"); $("#tree-content-holder").show("slide", { direction: "right" }, 150); $('.project-refs-form #path').val("#{@path}");