fix gfm autocomplete for usernames

This commit is contained in:
Cyril 2012-12-11 00:04:47 +01:00
parent d3b8952f8f
commit a5861b8ff3

View file

@ -1,52 +1,58 @@
# Creates the variables for setting up GFM auto-completion # Creates the variables for setting up GFM auto-completion
window.GitLab ?= {} window.GitLab ?= {}
GitLab.GfmAutoComplete ?= {} GitLab.GfmAutoComplete =
# Emoji
data = []
template = "<li data-value='${insert}'>${name} <img alt='${name}' height='20' src='${image}' width='20' /></li>"
GitLab.GfmAutoComplete.Emoji = {data, template}
# Team Members
data = []
url = '';
params = {private_token: '', page: 1}
GitLab.GfmAutoComplete.Members = {data, url, params}
# Add GFM auto-completion to all input fields, that accept GFM input.
GitLab.GfmAutoComplete.setup = ->
input = $('.js-gfm-input')
# Emoji # Emoji
input.atWho ':', Emoji:
data: GitLab.GfmAutoComplete.Emoji.data, data: []
tpl: GitLab.GfmAutoComplete.Emoji.template template: '<li data-value="${insert}">${name} <img alt="${name}" height="20" src="${image}" width="20" /></li>'
# Team Members # Team Members
input.atWho '@', (query, callback) -> Members:
(getMoreMembers = -> data: []
$.getJSON(GitLab.GfmAutoComplete.Members.url, GitLab.GfmAutoComplete.Members.params) url: ''
.success (members) -> params:
# pick the data we need private_token: ''
newMembersData = $.map(members, (m) -> m.name ) page: 1
template: '<li data-value="${username}">${username} <small>${name}</small></li>'
# add the new page of data to the rest # Add GFM auto-completion to all input fields, that accept GFM input.
$.merge(GitLab.GfmAutoComplete.Members.data, newMembersData) setup: ->
input = $('.js-gfm-input')
# show the pop-up with a copy of the current data # Emoji
callback(GitLab.GfmAutoComplete.Members.data[..]) input.atWho ':',
data: @Emoji.data
tpl: @Emoji.template
# are we past the last page? # Team Members
if newMembersData.length is 0 input.atWho '@',
# set static data and stop callbacks tpl: @Members.template
input.atWho '@', callback: (query, callback) =>
data: GitLab.GfmAutoComplete.Members.data (getMoreMembers = =>
callback: null $.getJSON(@Members.url, @Members.params).done (members) =>
else # pick the data we need
# get next page newMembersData = $.map(members, (m) ->
getMoreMembers() username: m.username
name: m.name
)
# so the next request gets the next page # add the new page of data to the rest
GitLab.GfmAutoComplete.Members.params.page += 1 $.merge(@Members.data, newMembersData)
).call()
# show the pop-up with a copy of the current data
callback(@Members.data[..])
# are we past the last page?
if newMembersData.length is 0
# set static data and stop callbacks
input.atWho '@',
data: @Members.data
callback: null
else
# get next page
getMoreMembers()
# so the next callback requests the next page
@Members.params.page += 1
).call()