From 059c243196ec1f5e945719cd09c6fa5ac5ded35c Mon Sep 17 00:00:00 2001 From: Connor Shea Date: Fri, 27 May 2016 14:53:18 -0600 Subject: [PATCH 1/3] Separate emoji from the main autocomplete_sources file. Emoji are entirely static most of the time, so there's no reason to include them along with a bunch of dynamically-generated content on every page load. In the future, hopefully, the emoji autocomplete JSON will be cached long-term and gzip'd, which would save quite a bit of bandwidth. --- .../javascripts/gfm_auto_complete.js.coffee | 62 ++++++++++++++++--- app/assets/javascripts/gl_form.js.coffee | 1 + app/controllers/projects_controller.rb | 11 +++- .../layouts/_init_auto_complete.html.haml | 4 +- config/routes.rb | 1 + 5 files changed, 67 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/gfm_auto_complete.js.coffee b/app/assets/javascripts/gfm_auto_complete.js.coffee index 41dba342107f..93e0b7cacb23 100644 --- a/app/assets/javascripts/gfm_auto_complete.js.coffee +++ b/app/assets/javascripts/gfm_auto_complete.js.coffee @@ -6,10 +6,6 @@ GitLab.GfmAutoComplete = dataSource: '' - # Emoji - Emoji: - template: '
  • ${name} ${name}
  • ' - # Team Members Members: template: '
  • ${username} ${title}
  • ' @@ -50,12 +46,6 @@ GitLab.GfmAutoComplete = setupAtWho: -> - # Emoji - @input.atwho - at: ':' - displayTpl: @Emoji.template - insertTpl: ':${name}:' - # Team Members @input.atwho at: '@' @@ -126,5 +116,57 @@ GitLab.GfmAutoComplete = @input.atwho 'load', 'milestones', data.milestones # load merge requests @input.atwho 'load', 'mergerequests', data.mergerequests + +GitLab.GfmAutoCompleteEmoji = + dataLoading: false + + dataSource: '' + + # Emoji + Emoji: + template: '
  • ${name} ${name}
  • ' + + # Add GFM auto-completion to all input fields, that accept GFM input. + setup: (wrap) -> + @input = $('.js-gfm-input') + + # destroy previous instances + @destroyAtWho() + + # set up instances + @setupAtWho() + + if @dataSource + console.log(@dataSource) + if !@dataLoading + @dataLoading = true + + # We should wait until initializations are done + # and only trigger the last .setup since + # The previous .dataSource belongs to the previous issuable + # and the last one will have the **proper** .dataSource property + # TODO: Make this a singleton and turn off events when moving to another page + setTimeout( => + fetch = @fetchData(@dataSource) + fetch.done (data) => + @dataLoading = false + @loadData(data) + , 1000) + + + setupAtWho: -> + # Emoji + @input.atwho + at: ':' + displayTpl: @Emoji.template + insertTpl: ':${name}:' + + destroyAtWho: -> + @input.atwho('destroy') + + fetchData: (dataSource) -> + $.getJSON(dataSource) + + loadData: (data) -> # load emojis @input.atwho 'load', ':', data.emojis diff --git a/app/assets/javascripts/gl_form.js.coffee b/app/assets/javascripts/gl_form.js.coffee index d540cc4dc467..b4da66e83e5c 100644 --- a/app/assets/javascripts/gl_form.js.coffee +++ b/app/assets/javascripts/gl_form.js.coffee @@ -27,6 +27,7 @@ class @GLForm # remove notify commit author checkbox for non-commit notes GitLab.GfmAutoComplete.setup() + GitLab.GfmAutoCompleteEmoji.setup() new DropzoneInput(@form) autosize(@textarea) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 3af62c7696c8..d79ac9c4b003 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -139,7 +139,6 @@ def autocomplete_sources participants = ::Projects::ParticipantsService.new(@project, current_user).execute(note_type, note_id) @suggestions = { - emojis: Gitlab::AwardEmoji.urls, issues: autocomplete.issues, milestones: autocomplete.milestones, mergerequests: autocomplete.merge_requests, @@ -151,6 +150,16 @@ def autocomplete_sources end end + def autocomplete_sources_emoji + @suggestions = { + emojis: AwardEmoji.urls + } + + respond_to do |format| + format.json { render json: @suggestions } + end + end + def archive return access_denied! unless can?(current_user, :archive_project, @project) diff --git a/app/views/layouts/_init_auto_complete.html.haml b/app/views/layouts/_init_auto_complete.html.haml index 96b384854259..2986cb8ed3f6 100644 --- a/app/views/layouts/_init_auto_complete.html.haml +++ b/app/views/layouts/_init_auto_complete.html.haml @@ -2,5 +2,7 @@ - if @noteable :javascript - GitLab.GfmAutoComplete.dataSource = "#{autocomplete_sources_namespace_project_path(project.namespace, project, type: @noteable.class, type_id: params[:id])}" + GitLab.GfmAutoComplete.dataSource = "#{autocomplete_sources_namespace_project_path(project.namespace, project, type: @noteable.class)}" GitLab.GfmAutoComplete.setup(); + GitLab.GfmAutoCompleteEmoji.dataSource = "#{autocomplete_sources_emoji_namespace_project_path(project.namespace, project, type: @noteable.class)}" + GitLab.GfmAutoCompleteEmoji.setup(); diff --git a/config/routes.rb b/config/routes.rb index 27ab79d68f5d..05aef5a5e7ce 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -450,6 +450,7 @@ post :toggle_star post :markdown_preview get :autocomplete_sources + get :autocomplete_sources_emoji get :activity end -- GitLab From b9f59ec39c53c7bb0bf26bf8adcf99afd5177490 Mon Sep 17 00:00:00 2001 From: Connor Shea Date: Fri, 27 May 2016 15:15:33 -0600 Subject: [PATCH 2/3] Remove autocomplete_sources_emoji route. Instead add a JSON response for the /emojis path and pull data from that. Much cleaner this way. --- app/assets/javascripts/gfm_auto_complete.js.coffee | 2 +- app/controllers/emojis_controller.rb | 8 ++++++++ app/controllers/projects_controller.rb | 10 ---------- app/views/layouts/_init_auto_complete.html.haml | 4 ++-- config/routes.rb | 1 - 5 files changed, 11 insertions(+), 14 deletions(-) diff --git a/app/assets/javascripts/gfm_auto_complete.js.coffee b/app/assets/javascripts/gfm_auto_complete.js.coffee index 93e0b7cacb23..f4170a769130 100644 --- a/app/assets/javascripts/gfm_auto_complete.js.coffee +++ b/app/assets/javascripts/gfm_auto_complete.js.coffee @@ -169,4 +169,4 @@ GitLab.GfmAutoCompleteEmoji = loadData: (data) -> # load emojis - @input.atwho 'load', ':', data.emojis + @input.atwho 'load', ':', data.emoji diff --git a/app/controllers/emojis_controller.rb b/app/controllers/emojis_controller.rb index 1bec5a7d27ff..32e415869926 100644 --- a/app/controllers/emojis_controller.rb +++ b/app/controllers/emojis_controller.rb @@ -2,5 +2,13 @@ class EmojisController < ApplicationController layout false def index + @emoji = { + emoji: AwardEmoji.urls + } + + respond_to do |format| + format.html + format.json { render json: @emoji } + end end end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index d79ac9c4b003..19c71634e63e 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -150,16 +150,6 @@ def autocomplete_sources end end - def autocomplete_sources_emoji - @suggestions = { - emojis: AwardEmoji.urls - } - - respond_to do |format| - format.json { render json: @suggestions } - end - end - def archive return access_denied! unless can?(current_user, :archive_project, @project) diff --git a/app/views/layouts/_init_auto_complete.html.haml b/app/views/layouts/_init_auto_complete.html.haml index 2986cb8ed3f6..b554081759dd 100644 --- a/app/views/layouts/_init_auto_complete.html.haml +++ b/app/views/layouts/_init_auto_complete.html.haml @@ -2,7 +2,7 @@ - if @noteable :javascript - GitLab.GfmAutoComplete.dataSource = "#{autocomplete_sources_namespace_project_path(project.namespace, project, type: @noteable.class)}" + GitLab.GfmAutoComplete.dataSource = "#{autocomplete_sources_namespace_project_path(project.namespace, project, type: @noteable.class, type_id: params[:id])}" GitLab.GfmAutoComplete.setup(); - GitLab.GfmAutoCompleteEmoji.dataSource = "#{autocomplete_sources_emoji_namespace_project_path(project.namespace, project, type: @noteable.class)}" + GitLab.GfmAutoCompleteEmoji.dataSource = "#{emojis_path}" GitLab.GfmAutoCompleteEmoji.setup(); diff --git a/config/routes.rb b/config/routes.rb index 05aef5a5e7ce..27ab79d68f5d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -450,7 +450,6 @@ post :toggle_star post :markdown_preview get :autocomplete_sources - get :autocomplete_sources_emoji get :activity end -- GitLab From f385aebeff74a959137dde64f06ee1ef56212de4 Mon Sep 17 00:00:00 2001 From: Connor Shea Date: Mon, 30 May 2016 13:04:04 -0600 Subject: [PATCH 3/3] Fix tests failing. --- app/assets/javascripts/gfm_auto_complete.js.coffee | 7 ------- app/controllers/emojis_controller.rb | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/app/assets/javascripts/gfm_auto_complete.js.coffee b/app/assets/javascripts/gfm_auto_complete.js.coffee index f4170a769130..75b5a71f7509 100644 --- a/app/assets/javascripts/gfm_auto_complete.js.coffee +++ b/app/assets/javascripts/gfm_auto_complete.js.coffee @@ -130,14 +130,10 @@ GitLab.GfmAutoCompleteEmoji = setup: (wrap) -> @input = $('.js-gfm-input') - # destroy previous instances - @destroyAtWho() - # set up instances @setupAtWho() if @dataSource - console.log(@dataSource) if !@dataLoading @dataLoading = true @@ -161,9 +157,6 @@ GitLab.GfmAutoCompleteEmoji = displayTpl: @Emoji.template insertTpl: ':${name}:' - destroyAtWho: -> - @input.atwho('destroy') - fetchData: (dataSource) -> $.getJSON(dataSource) diff --git a/app/controllers/emojis_controller.rb b/app/controllers/emojis_controller.rb index 32e415869926..86de74b3fa19 100644 --- a/app/controllers/emojis_controller.rb +++ b/app/controllers/emojis_controller.rb @@ -3,7 +3,7 @@ class EmojisController < ApplicationController def index @emoji = { - emoji: AwardEmoji.urls + emoji: Gitlab::AwardEmoji.urls } respond_to do |format| -- GitLab