BigW Consortium Gitlab

tree_helper.rb 2.72 KB
Newer Older
1
module TreeHelper
2 3 4 5
  # 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
6
  def render_tree(tree)
7
    # Render Folders before Files/Submodules
8
    folders, files, submodules = tree.trees, tree.blobs, tree.submodules
9 10 11 12

    tree = ""

    # Render folders if we have any
13
    tree += render partial: 'projects/tree/tree_item', collection: folders, locals: {type: 'folder'} if folders.present?
14

15
    # Render files if we have any
16
    tree += render partial: 'projects/tree/blob_item', collection: files, locals: {type: 'file'} if files.present?
17 18

    # Render submodules if we have any
19
    tree += render partial: 'projects/tree/submodule_item', collection: submodules if submodules.present?
20 21

    tree.html_safe
22 23
  end

24 25 26 27 28 29
  # 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')
30 31
  end

32 33
  def tree_hex_class(content)
    "file_#{hexdigest(content.name)}"
34
  end
35 36 37 38 39 40 41

  # Public: Determines if a given filename is compatible with GitHub::Markup.
  #
  # filename - Filename string to check
  #
  # Returns boolean
  def markup?(filename)
42
    filename.downcase.end_with?(*%w(.textile .rdoc .org .creole
43
                                    .mediawiki .rst .adoc .asciidoc .pod))
44 45 46
  end

  def gitlab_markdown?(filename)
47
    filename.downcase.end_with?(*%w(.mdown .md .markdown))
48
  end
49

randx committed
50
  def plain_text_readme? filename
51
    filename =~ /^README(.txt)?$/i
randx committed
52 53
  end

54 55 56 57
  # Simple shortcut to File.join
  def tree_join(*args)
    File.join(*args)
  end
58 59

  def allowed_tree_edit?
60 61
    return false unless @repository.branch_names.include?(@ref)

62 63 64 65 66 67
    if @project.protected_branch? @ref
      can?(current_user, :push_code_to_protected_branches, @project)
    else
      can?(current_user, :push_code, @project)
    end
  end
68

69
  def tree_breadcrumbs(tree, max_links = 2)
70
    if @path.present?
71
      part_path = ""
72
      parts = @path.split("\/")
73 74

      yield('..', nil) if parts.count > max_links
75

76 77 78
      parts.each do |part|
        part_path = File.join(part_path, part) unless part_path.empty?
        part_path = part if part_path.empty?
79

80
        next unless parts.last(2).include?(part) if parts.count > max_links
81
        yield(part, tree_join(@ref, part_path))
82 83
      end
    end
84
  end
85

86
  def up_dir_path tree
87 88
    file = File.join(@path, "..")
    tree_join(@ref, file)
89
  end
90 91 92 93

  def leave_edit_message
    "Leave edit mode?\nAll unsaved changes will be lost."
  end
skv-headless committed
94 95 96 97 98 99 100 101

  def editing_preview_title(filename)
    if gitlab_markdown?(filename) || markup?(filename)
      'Preview'
    else
      'Diff'
    end
  end
102
end