summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/shortcuts_issuable.coffee
blob: cefa1857d7fe031862943452bac793db3edebd99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#= require mousetrap
#= require shortcuts_navigation

class @ShortcutsIssuable extends ShortcutsNavigation
  constructor: (isMergeRequest) ->
    super()
    Mousetrap.bind('a', ->
      $('.block.assignee .edit-link').trigger('click')
      return false
    )
    Mousetrap.bind('m', ->
      $('.block.milestone .edit-link').trigger('click')
      return false
    )
    Mousetrap.bind('r', =>
      @replyWithSelectedText()
      return false
    )
    Mousetrap.bind('j', =>
      @prevIssue()
      return false
    )
    Mousetrap.bind('k', =>
      @nextIssue()
      return false
    )


    if isMergeRequest
      @enabledHelp.push('.hidden-shortcut.merge_requests')
    else
      @enabledHelp.push('.hidden-shortcut.issues')

  prevIssue: ->
    $prevBtn = $('.prev-btn')
    if not $prevBtn.hasClass('disabled')
      Turbolinks.visit($prevBtn.attr('href'))

  nextIssue: ->
    $nextBtn = $('.next-btn')
    if not $nextBtn.hasClass('disabled')
      Turbolinks.visit($nextBtn.attr('href'))

  replyWithSelectedText: ->
    if window.getSelection
      selected = window.getSelection().toString()
      replyField = $('.js-main-target-form #note_note')

      return if selected.trim() == ""

      # Put a '>' character before each non-empty line in the selection
      quote = _.map selected.split("\n"), (val) ->
        "> #{val}\n" if val.trim() != ''

      # If replyField already has some content, add a newline before our quote
      separator = replyField.val().trim() != "" and "\n" or ''

      replyField.val (_, current) ->
        current + separator + quote.join('') + "\n"

      # Trigger autosave for the added text
      replyField.trigger('input')

      # Focus the input field
      replyField.focus()