gitlabhq/app/assets/javascripts/gfm_auto_complete.js.coffee

59 lines
1.6 KiB
CoffeeScript
Raw Normal View History

# Creates the variables for setting up GFM auto-completion
2012-11-21 20:02:03 +01:00
window.GitLab ?= {}
2012-12-11 00:04:47 +01:00
GitLab.GfmAutoComplete =
# Emoji
2012-12-11 00:04:47 +01:00
Emoji:
data: []
template: '<li data-value="${insert}">${name} <img alt="${name}" height="20" src="${image}" width="20" /></li>'
# Team Members
2012-12-11 00:04:47 +01:00
Members:
data: []
url: ''
params:
private_token: ''
page: 1
template: '<li data-value="${username}">${username} <small>${name}</small></li>'
# Add GFM auto-completion to all input fields, that accept GFM input.
setup: ->
input = $('.js-gfm-input')
# Emoji
input.atWho ':',
data: @Emoji.data
tpl: @Emoji.template
# Team Members
input.atWho '@',
tpl: @Members.template
callback: (query, callback) =>
(getMoreMembers = =>
$.getJSON(@Members.url, @Members.params).done (members) =>
# pick the data we need
newMembersData = $.map(members, (m) ->
username: m.username
name: m.name
)
# add the new page of data to the rest
$.merge(@Members.data, newMembersData)
# 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()