cleanup coffeescript code for mentions auto-complete

This commit is contained in:
Nihad Abbasov 2012-10-16 04:01:40 -07:00
parent ec1bccb261
commit 57c134c39a
2 changed files with 38 additions and 48 deletions

View file

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

View file

@ -1,11 +1,11 @@
:javascript :javascript
$(function() { $(function() {
autocompleteMembersUrl = "#{ "/api/v2/projects/#{@project.code}/members" if @project }"; autocompleteMembers.url = "#{ "/api/v2/projects/#{@project.code}/members" if @project }";
autocompleteMembersParams.private_token = "#{current_user.authentication_token}"; autocompleteMembers.params.private_token = "#{current_user.private_token}";
autocompleteEmojiData = #{raw emoji_autocomplete_source}; autocompleteEmoji.data = #{raw emoji_autocomplete_source};
// convert the list so that the items have the right format for completion // convert the list so that the items have the right format for completion
autocompleteEmojiData = $.map(autocompleteEmojiData, function(value) { autocompleteEmoji.data = $.map(autocompleteEmoji.data, function(value) {
return { return {
name: value, name: value,
insert: value+':', insert: value+':',