tree.js.coffee 1.6 KB
Newer Older
C
Ciro Santilli 已提交
1
class @TreeView
2 3
  constructor: ->
    @initKeyNav()
R
Robert Speicher 已提交
4

5 6 7
    # Code browser tree slider
    # Make the entire tree-item row clickable, but not if clicking another link (like a commit message)
    $(".tree-content-holder .tree-item").on 'click', (e) ->
8
      $clickedEl = $(e.target)
9
      path = $('.tree-item-file-name a', this).attr('href')
10 11

      if not $clickedEl.is('a') and not $clickedEl.is('.str-truncated')
12 13 14 15 16
        if e.metaKey or e.which is 2
          e.preventDefault()
          window.open path, '_blank'
        else
          Turbolinks.visit path
R
Riyad Preukschas 已提交
17

18 19
    # Show the "Loading commit data" for only the first element
    $('span.log_loading:first').removeClass('hide')
R
Riyad Preukschas 已提交
20

21 22 23 24
  initKeyNav: ->
    li = $("tr.tree-item")
    liSelected = null
    $('body').keydown (e) ->
V
Valery Sizov 已提交
25 26 27
      if $("input:focus").length > 0 && (e.which == 38 || e.which == 40)
        return false

28 29 30 31 32 33 34 35
      if e.which is 40
        if liSelected
          next = liSelected.next()
          if next.length > 0
            liSelected.removeClass "selected"
            liSelected = next.addClass("selected")
        else
          liSelected = li.eq(0).addClass("selected")
R
Riyad Preukschas 已提交
36

37 38 39 40 41 42 43 44 45 46 47 48 49
        $(liSelected).focus()
      else if e.which is 38
        if liSelected
          next = liSelected.prev()
          if next.length > 0
            liSelected.removeClass "selected"
            liSelected = next.addClass("selected")
        else
          liSelected = li.last().addClass("selected")

        $(liSelected).focus()
      else if e.which is 13
        path = $('.tree-item.selected .tree-item-file-name a').attr('href')
V
Valery Sizov 已提交
50
        if path then Turbolinks.visit(path)