Commit a5861b8ff34621be246ea0951a9e46b8859af4d4

Authored by Cyril
1 parent d3b8952f

fix gfm autocomplete for usernames

app/assets/javascripts/gfm_auto_complete.js.coffee
1 # Creates the variables for setting up GFM auto-completion 1 # Creates the variables for setting up GFM auto-completion
2 2
3 window.GitLab ?= {} 3 window.GitLab ?= {}
4 -GitLab.GfmAutoComplete ?= {}  
5 -  
6 -# Emoji  
7 -data = []  
8 -template = "<li data-value='${insert}'>${name} <img alt='${name}' height='20' src='${image}' width='20' /></li>"  
9 -GitLab.GfmAutoComplete.Emoji = {data, template}  
10 -  
11 -# Team Members  
12 -data = []  
13 -url = '';  
14 -params = {private_token: '', page: 1}  
15 -GitLab.GfmAutoComplete.Members = {data, url, params}  
16 -  
17 -# Add GFM auto-completion to all input fields, that accept GFM input.  
18 -GitLab.GfmAutoComplete.setup = ->  
19 - input = $('.js-gfm-input')  
20 - 4 +GitLab.GfmAutoComplete =
21 # Emoji 5 # Emoji
22 - input.atWho ':',  
23 - data: GitLab.GfmAutoComplete.Emoji.data,  
24 - tpl: GitLab.GfmAutoComplete.Emoji.template 6 + Emoji:
  7 + data: []
  8 + template: '<li data-value="${insert}">${name} <img alt="${name}" height="20" src="${image}" width="20" /></li>'
25 9
26 # Team Members 10 # Team Members
27 - input.atWho '@', (query, callback) ->  
28 - (getMoreMembers = ->  
29 - $.getJSON(GitLab.GfmAutoComplete.Members.url, GitLab.GfmAutoComplete.Members.params)  
30 - .success (members) ->  
31 - # pick the data we need  
32 - newMembersData = $.map(members, (m) -> m.name )  
33 -  
34 - # add the new page of data to the rest  
35 - $.merge(GitLab.GfmAutoComplete.Members.data, newMembersData)  
36 -  
37 - # show the pop-up with a copy of the current data  
38 - callback(GitLab.GfmAutoComplete.Members.data[..])  
39 -  
40 - # are we past the last page?  
41 - if newMembersData.length is 0  
42 - # set static data and stop callbacks  
43 - input.atWho '@',  
44 - data: GitLab.GfmAutoComplete.Members.data  
45 - callback: null  
46 - else  
47 - # get next page  
48 - getMoreMembers()  
49 -  
50 - # so the next request gets the next page  
51 - GitLab.GfmAutoComplete.Members.params.page += 1  
52 - ).call() 11 + Members:
  12 + data: []
  13 + url: ''
  14 + params:
  15 + private_token: ''
  16 + page: 1
  17 + template: '<li data-value="${username}">${username} <small>${name}</small></li>'
  18 +
  19 + # Add GFM auto-completion to all input fields, that accept GFM input.
  20 + setup: ->
  21 + input = $('.js-gfm-input')
  22 +
  23 + # Emoji
  24 + input.atWho ':',
  25 + data: @Emoji.data
  26 + tpl: @Emoji.template
  27 +
  28 + # Team Members
  29 + input.atWho '@',
  30 + tpl: @Members.template
  31 + callback: (query, callback) =>
  32 + (getMoreMembers = =>
  33 + $.getJSON(@Members.url, @Members.params).done (members) =>
  34 + # pick the data we need
  35 + newMembersData = $.map(members, (m) ->
  36 + username: m.username
  37 + name: m.name
  38 + )
  39 +
  40 + # add the new page of data to the rest
  41 + $.merge(@Members.data, newMembersData)
  42 +
  43 + # show the pop-up with a copy of the current data
  44 + callback(@Members.data[..])
  45 +
  46 + # are we past the last page?
  47 + if newMembersData.length is 0
  48 + # set static data and stop callbacks
  49 + input.atWho '@',
  50 + data: @Members.data
  51 + callback: null
  52 + else
  53 + # get next page
  54 + getMoreMembers()
  55 +
  56 + # so the next callback requests the next page
  57 + @Members.params.page += 1
  58 + ).call()