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

# Emoji
4 5 6
data      = []
template  = "<li data-value='${insert}'>${name} <img alt='${name}' height='20' src='${image}' width='20' /></li>"
window.autocompleteEmoji = {data, template}
7 8

# Team Members
9 10 11
url     = '';
params  = {private_token: '', page: 1}
window.autocompleteMembers = {data, url, params}
12

13 14 15
# Add GFM auto-completion to all input fields, that accept GFM input.
window.setupGfmAutoComplete = ->
  $input = $('.js-gfm-input')
16

17 18 19 20
  # Emoji
  $input.atWho ':',
    data: autocompleteEmoji.data,
    tpl: autocompleteEmoji.template
21

22 23
  # Team Members
  $input.atWho '@', (query, callback) ->
24
    (getMoreMembers = ->
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
      $.getJSON(autocompleteMembers.url, autocompleteMembers.params).success (members) ->
        # pick the data we need
        newMembersData = $.map members, (m) -> m.name

        # add the new page of data to the rest
        $.merge autocompleteMembers.data, newMembersData

        # show the pop-up with a copy of the current data
        callback autocompleteMembers.data[..]

        # are we past the last page?
        if newMembersData.length is 0
          # set static data and stop callbacks
          $input.atWho '@',
            data: autocompleteMembers.data
            callback: null
        else
          # get next page
          getMoreMembers()
44 45

      # so the next request gets the next page
46 47
      autocompleteMembers.params.page += 1
    ).call()