BigW Consortium Gitlab

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

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

7
  dataSource: ''
8

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

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

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

21
  # Add GFM auto-completion to all input fields, that accept GFM input.
22 23 24 25 26 27 28 29 30 31 32 33
  setup: (wrap) ->
    @input = $('.js-gfm-input')

    # destroy previous instances
    @destroyAtWho()

    # set up instances
    @setupAtWho()

    if @dataSource
      if !@dataLoading
        @dataLoading = true
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48
        # We should wait until initializations are done
        # and only trigger the last .setup since
        # The previous .dataSource belongs to the previous issuable
        # and the last one will have the **proper** .dataSource property
        # TODO: Make this a singleton and turn off events when moving to another page
        setTimeout( =>
          fetch = @fetchData(@dataSource)
          fetch.done (data) =>
            @dataLoading = false
            @loadData(data)
        , 1000)


  setupAtWho: ->
49
    # Emoji
50
    @input.atwho
51
      at: ':'
52 53
      displayTpl: @Emoji.template
      insertTpl: ':${name}:'
54 55

    # Team Members
56
    @input.atwho
57
      at: '@'
58 59 60
      displayTpl: @Members.template
      insertTpl: '${atwho-at}${username}'
      searchKey: 'search'
61
      callbacks:
62
        beforeSave: (members) ->
63
          $.map members, (m) ->
64 65 66 67 68 69
            title = m.name
            title += " (#{m.count})" if m.count

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

71
    @input.atwho
72 73
      at: '#'
      alias: 'issues'
74 75 76
      searchKey: 'search'
      displayTpl: @Issues.template
      insertTpl: '${atwho-at}${id}'
77
      callbacks:
78
        beforeSave: (issues) ->
79
          $.map issues, (i) ->
80 81 82
            id:     i.iid
            title:  sanitize(i.title)
            search: "#{i.iid} #{i.title}"
83

84
    @input.atwho
85 86
      at: '!'
      alias: 'mergerequests'
87 88 89
      searchKey: 'search'
      displayTpl: @Issues.template
      insertTpl: '${atwho-at}${id}'
90
      callbacks:
91
        beforeSave: (merges) ->
92
          $.map merges, (m) ->
93 94 95
            id:     m.iid
            title:  sanitize(m.title)
            search: "#{m.iid} #{m.title}"
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  destroyAtWho: ->
    @input.atwho('destroy')

  fetchData: (dataSource) ->
    $.getJSON(dataSource)

  loadData: (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