From 439cd3ebd3613825fc426238770f522a9401d51e Mon Sep 17 00:00:00 2001 From: ichord Date: Fri, 3 May 2013 23:39:18 +0800 Subject: [PATCH] autocomplete issues and preload autocomplete data. update jquery-atwho-rails to v0.3.0 add autocomplete_source action to project for gfm autocomplete move init_autocomplete layout from head_panel to project_resource --- Gemfile | 2 +- Gemfile.lock | 4 +- .../javascripts/gfm_auto_complete.js.coffee | 50 +++++++++++++------ app/controllers/projects_controller.rb | 12 +++++ app/views/layouts/_head_panel.html.haml | 2 - .../layouts/_init_auto_complete.html.haml | 15 +----- app/views/layouts/project_resource.html.haml | 1 + config/routes.rb | 1 + spec/routing/project_routing_spec.rb | 4 ++ 9 files changed, 57 insertions(+), 34 deletions(-) diff --git a/Gemfile b/Gemfile index bccde6664dd..1496288029d 100644 --- a/Gemfile +++ b/Gemfile @@ -112,7 +112,7 @@ group :assets do gem 'chosen-rails', "0.9.8" gem 'select2-rails' - gem 'jquery-atwho-rails', "0.1.7" + gem 'jquery-atwho-rails', "0.3.0" gem "jquery-rails", "2.1.3" gem "jquery-ui-rails", "2.0.2" gem "modernizr", "2.6.2" diff --git a/Gemfile.lock b/Gemfile.lock index c06a1bd09cc..d9418599387 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -231,7 +231,7 @@ GEM httpauth (0.2.0) i18n (0.6.1) journey (1.0.4) - jquery-atwho-rails (0.1.7) + jquery-atwho-rails (0.3.0) jquery-rails (2.1.3) railties (>= 3.1.0, < 5.0) thor (~> 0.14) @@ -531,7 +531,7 @@ DEPENDENCIES guard-spinach haml-rails httparty - jquery-atwho-rails (= 0.1.7) + jquery-atwho-rails (= 0.3.0) jquery-rails (= 2.1.3) jquery-turbolinks jquery-ui-rails (= 2.0.2) diff --git a/app/assets/javascripts/gfm_auto_complete.js.coffee b/app/assets/javascripts/gfm_auto_complete.js.coffee index 9ef194fbd25..c8c57b91e03 100644 --- a/app/assets/javascripts/gfm_auto_complete.js.coffee +++ b/app/assets/javascripts/gfm_auto_complete.js.coffee @@ -2,37 +2,55 @@ window.GitLab ?= {} GitLab.GfmAutoComplete = + # private_token: '' + dataSource: '' # Emoji Emoji: - data: [] + assetBase: '' template: '
  • ${name} ${name}
  • ' # Team Members Members: - data: [] - url: '' - params: - private_token: '' template: '
  • ${username} ${name}
  • ' + Issues: + template: '
  • ${id} ${title}
  • ' + # Add GFM auto-completion to all input fields, that accept GFM input. setup: -> input = $('.js-gfm-input') # Emoji - input.atWho '(?:^|\\s):', - data: @Emoji.data + input.atwho + at: ':' tpl: @Emoji.template + callbacks: + before_save: (emojis) => + $.map emojis, (em) => name: em, insert: em+ ':', image: "#{@Emoji.assetBase}/#{em}.png" # Team Members - input.atWho '@', + input.atwho + at: '@' tpl: @Members.template - callback: (query, callback) => - request_params = $.extend({}, @Members.params, query: query) - $.getJSON(@Members.url, request_params).done (members) => - new_members_data = $.map(members, (m) -> - username: m.username, - name: m.name - ) - callback(new_members_data) + search_key: 'search' + callbacks: + before_save: (members) => + $.map members, (m) => name: m.name, username: m.username, search: "#{m.username} #{m.name}" + input.atwho + at: '#' + alias: 'issues' + search_key: 'search' + tpl: @Issues.template + callbacks: + before_save: (issues) -> + $.map issues, (i) -> id: i.id, title: i.title, search: "#{i.id} #{i.title}" + + input.one "focus", => + $.getJSON(@dataSource).done (data) -> + # load members + input.atwho 'load', "@", data.members + # load issues + input.atwho 'load', "issues", data.issues + # load emojis + input.atwho 'load', ":", data.emojis diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 255baba0ecb..0e05213b797 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -93,4 +93,16 @@ class ProjectsController < ProjectResourceController format.js end end + + def autocomplete_sources + @suggestions = { + emojis: Emoji.names, + issues: @project.issues.select([:id, :title, :description]), + members: @project.users.select([:username, :name]).order(:username) + } + + respond_to do |format| + format.json { render :json => @suggestions } + end + end end diff --git a/app/views/layouts/_head_panel.html.haml b/app/views/layouts/_head_panel.html.haml index b43650459d8..2ea6c3e46d9 100644 --- a/app/views/layouts/_head_panel.html.haml +++ b/app/views/layouts/_head_panel.html.haml @@ -36,5 +36,3 @@ = link_to current_user, class: "profile-pic" do = image_tag gravatar_icon(current_user.email, 26) - -= render "layouts/init_auto_complete" diff --git a/app/views/layouts/_init_auto_complete.html.haml b/app/views/layouts/_init_auto_complete.html.haml index 8f8c7d8885e..3549794b90d 100644 --- a/app/views/layouts/_init_auto_complete.html.haml +++ b/app/views/layouts/_init_auto_complete.html.haml @@ -1,17 +1,6 @@ :javascript $(function() { - GitLab.GfmAutoComplete.Members.url = "#{ "/api/v3/projects/#{@project.id}/members" if @project }"; - GitLab.GfmAutoComplete.Members.params.private_token = "#{current_user.private_token}"; - - GitLab.GfmAutoComplete.Emoji.data = #{raw emoji_autocomplete_source}; - // convert the list so that the items have the right format for completion - GitLab.GfmAutoComplete.Emoji.data = $.map(GitLab.GfmAutoComplete.Emoji.data, function(value) { - return { - name: value, - insert: value+':', - image: '#{image_path("emoji")}/'+value+'.png' - } - }); - + GitLab.GfmAutoComplete.dataSource = "#{autocomplete_sources_project_path(@project)}" + GitLab.GfmAutoComplete.Emoji.assetBase = '#{image_path("emoji")}' GitLab.GfmAutoComplete.setup(); }); diff --git a/app/views/layouts/project_resource.html.haml b/app/views/layouts/project_resource.html.haml index 1fc36410668..6d8bf9b710b 100644 --- a/app/views/layouts/project_resource.html.haml +++ b/app/views/layouts/project_resource.html.haml @@ -3,6 +3,7 @@ = render "layouts/head", title: @project.name_with_namespace %body{class: "#{app_theme} project", :'data-page' => body_data_page, :'data-project-id' => @project.id } = render "layouts/head_panel", title: project_title(@project) + = render "layouts/init_auto_complete" = render "layouts/flash" - if can?(current_user, :download_code, @project) = render 'shared/no_ssh' diff --git a/config/routes.rb b/config/routes.rb index 8bd6307357a..9ddca262027 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -168,6 +168,7 @@ Gitlab::Application.routes.draw do member do put :transfer post :fork + get :autocomplete_sources end resources :blob, only: [:show], constraints: {id: /.+/} diff --git a/spec/routing/project_routing_spec.rb b/spec/routing/project_routing_spec.rb index dd4fb54af69..15d774a899b 100644 --- a/spec/routing/project_routing_spec.rb +++ b/spec/routing/project_routing_spec.rb @@ -83,6 +83,10 @@ describe ProjectsController, "routing" do get("/gitlabhq/edit").should route_to('projects#edit', id: 'gitlabhq') end + it "to #autocomplete_sources" do + get('/gitlabhq/autocomplete_sources').should route_to('projects#autocomplete_sources', id: "gitlabhq") + end + it "to #show" do get("/gitlabhq").should route_to('projects#show', id: 'gitlabhq') end