summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/gfm_auto_complete.js.coffee
blob: 7967892f8562b9052ae4ec994e774918cfa849c7 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Creates the variables for setting up GFM auto-completion

window.GitLab ?= {}
GitLab.GfmAutoComplete =
  dataSource: ''

  # Emoji
  Emoji:
    template: '<li>${name} <img alt="${name}" height="20" src="${path}" width="20" /></li>'

  # Team Members
  Members:
    template: '<li>${username} <small>${title}</small></li>'

  # Issues and MergeRequests
  Issues:
    template: '<li><small>${id}</small> ${title}</li>'

  # Add GFM auto-completion to all input fields, that accept GFM input.
  setup: ->
    input = $('.js-gfm-input')

    # Emoji
    input.atwho
      at: ':'
      displayTpl: @Emoji.template
      insertTpl: ':${name}:'

    # Team Members
    input.atwho
      at: '@'
      displayTpl: @Members.template
      insertTpl: '${atwho-at}${username}'
      searchKey: 'search'
      callbacks:
        beforeSave: (members) ->
          $.map members, (m) -> 
            title = m.name
            title += " (#{m.count})" if m.count

            username: m.username
            title:    sanitize(title)
            search:   sanitize("#{m.username} #{m.name}")

    input.atwho
      at: '#'
      alias: 'issues'
      searchKey: 'search'
      displayTpl: @Issues.template
      insertTpl: '${atwho-at}${id}'
      callbacks:
        beforeSave: (issues) ->
          $.map issues, (i) -> 
            id:     i.iid
            title:  sanitize(i.title)
            search: "#{i.iid} #{i.title}"

    input.atwho
      at: '!'
      alias: 'mergerequests'
      searchKey: 'search'
      displayTpl: @Issues.template
      insertTpl: '${atwho-at}${id}'
      callbacks:
        beforeSave: (merges) ->
          $.map merges, (m) -> 
            id:     m.iid
            title:  sanitize(m.title)
            search: "#{m.iid} #{m.title}"

    input.one 'focus', =>
      $.getJSON(@dataSource).done (data) ->
        # load members
        input.atwho 'load', '@', data.members
        # load issues
        input.atwho 'load', 'issues', data.issues
        # load merge requests
        input.atwho 'load', 'mergerequests', data.mergerequests
        # load emojis
        input.atwho 'load', ':', data.emojis