BigW Consortium Gitlab

gfm_auto_complete.js.coffee 2.09 KB
Newer Older
1
# Creates the variables for setting up GFM auto-completion
2

Riyad Preukschas committed
3
window.GitLab ?= {}
4
GitLab.GfmAutoComplete =
5
  dataSource: ''
6

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

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

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

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

    # Emoji
24 25
    input.atwho
      at: ':'
26 27
      displayTpl: @Emoji.template
      insertTpl: ':${name}:'
28 29

    # Team Members
30 31
    input.atwho
      at: '@'
32 33 34
      displayTpl: @Members.template
      insertTpl: '${atwho-at}${username}'
      searchKey: 'search'
35
      callbacks:
36
        beforeSave: (members) ->
37 38 39 40 41 42 43
          $.map members, (m) -> 
            title = m.name
            title += " (#{m.count})" if m.count

            username: m.username
            title:    sanitize(title)
            search:   sanitize("#{m.username} #{m.name}")
44 45 46 47

    input.atwho
      at: '#'
      alias: 'issues'
48 49 50
      searchKey: 'search'
      displayTpl: @Issues.template
      insertTpl: '${atwho-at}${id}'
51
      callbacks:
52
        beforeSave: (issues) ->
53 54 55 56
          $.map issues, (i) -> 
            id:     i.iid
            title:  sanitize(i.title)
            search: "#{i.iid} #{i.title}"
57

58 59 60
    input.atwho
      at: '!'
      alias: 'mergerequests'
61 62 63
      searchKey: 'search'
      displayTpl: @Issues.template
      insertTpl: '${atwho-at}${id}'
64
      callbacks:
65
        beforeSave: (merges) ->
66 67 68 69
          $.map merges, (m) -> 
            id:     m.iid
            title:  sanitize(m.title)
            search: "#{m.iid} #{m.title}"
70

71
    input.one 'focus', =>
72 73
      $.getJSON(@dataSource).done (data) ->
        # load members
74
        input.atwho 'load', '@', data.members
75
        # load issues
76
        input.atwho 'load', 'issues', data.issues
77
        # load merge requests
78
        input.atwho 'load', 'mergerequests', data.mergerequests
79
        # load emojis
80
        input.atwho 'load', ':', data.emojis