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

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