From 57c134c39a106a3e9b9de776c06fe971b83a8224 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Tue, 16 Oct 2012 04:01:40 -0700 Subject: [PATCH] cleanup coffeescript code for mentions auto-complete --- .../javascripts/gfm_auto_complete.js.coffee | 78 ++++++++----------- .../layouts/_init_auto_complete.html.haml | 8 +- 2 files changed, 38 insertions(+), 48 deletions(-) diff --git a/app/assets/javascripts/gfm_auto_complete.js.coffee b/app/assets/javascripts/gfm_auto_complete.js.coffee index f68a029d..6c0c8886 100644 --- a/app/assets/javascripts/gfm_auto_complete.js.coffee +++ b/app/assets/javascripts/gfm_auto_complete.js.coffee @@ -1,57 +1,47 @@ +# Creates the variables for setting up GFM auto-completion -### - Creates the variables for setting up GFM auto-completion -### # Emoji -window.autocompleteEmojiData = []; -window.autocompleteEmojiTemplate = "
  • ${name} ${name}
  • "; +data = [] +template = "
  • ${name} ${name}
  • " +window.autocompleteEmoji = {data, template} # Team Members -window.autocompleteMembersUrl = ""; -window.autocompleteMembersParams = - private_token: "" - page: 1 -window.autocompleteMembersData = []; +url = ''; +params = {private_token: '', page: 1} +window.autocompleteMembers = {data, url, params} - - -### - 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 = -> - ### - Emoji - ### - $('.js-gfm-input').atWho ':', - data: autocompleteEmojiData, - tpl: autocompleteEmojiTemplate + $input = $('.js-gfm-input') - ### - Team Members - ### - $('.js-gfm-input').atWho '@', (query, callback) -> + # Emoji + $input.atWho ':', + data: autocompleteEmoji.data, + tpl: autocompleteEmoji.template + + # Team Members + $input.atWho '@', (query, callback) -> (getMoreMembers = -> - $.getJSON(autocompleteMembersUrl, autocompleteMembersParams) - .success (members) -> - # pick the data we need - newMembersData = $.map members, (m) -> m.name + $.getJSON(autocompleteMembers.url, autocompleteMembers.params).success (members) -> + # pick the data we need + newMembersData = $.map members, (m) -> m.name - # add the new page of data to the rest - $.merge autocompleteMembersData, newMembersData + # add the new page of data to the rest + $.merge autocompleteMembers.data, newMembersData - # show the pop-up with a copy of the current data - callback autocompleteMembersData[..] + # show the pop-up with a copy of the current data + callback autocompleteMembers.data[..] - # are we past the last page? - if newMembersData.length == 0 - # set static data and stop callbacks - $('.js-gfm-input').atWho '@', - data: autocompleteMembersData - callback: null - else - # get next page - getMoreMembers() + # are we past the last page? + if newMembersData.length is 0 + # set static data and stop callbacks + $input.atWho '@', + data: autocompleteMembers.data + callback: null + else + # get next page + getMoreMembers() # so the next request gets the next page - autocompleteMembersParams.page += 1; - ).call(); + autocompleteMembers.params.page += 1 + ).call() diff --git a/app/views/layouts/_init_auto_complete.html.haml b/app/views/layouts/_init_auto_complete.html.haml index 87a74655..9eadc18c 100644 --- a/app/views/layouts/_init_auto_complete.html.haml +++ b/app/views/layouts/_init_auto_complete.html.haml @@ -1,11 +1,11 @@ :javascript $(function() { - autocompleteMembersUrl = "#{ "/api/v2/projects/#{@project.code}/members" if @project }"; - autocompleteMembersParams.private_token = "#{current_user.authentication_token}"; + autocompleteMembers.url = "#{ "/api/v2/projects/#{@project.code}/members" if @project }"; + 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 - autocompleteEmojiData = $.map(autocompleteEmojiData, function(value) { + autocompleteEmoji.data = $.map(autocompleteEmoji.data, function(value) { return { name: value, insert: value+':',