提交 86887893 编写于 作者: D Dmitriy Zaporozhets

Merge pull request #3834 from ichord/gfm-autocomp

improve gfm autocomplete
...@@ -112,7 +112,7 @@ group :assets do ...@@ -112,7 +112,7 @@ group :assets do
gem 'chosen-rails', "0.9.8" gem 'chosen-rails', "0.9.8"
gem 'select2-rails' 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-rails", "2.1.3"
gem "jquery-ui-rails", "2.0.2" gem "jquery-ui-rails", "2.0.2"
gem "modernizr", "2.6.2" gem "modernizr", "2.6.2"
......
...@@ -231,7 +231,7 @@ GEM ...@@ -231,7 +231,7 @@ GEM
httpauth (0.2.0) httpauth (0.2.0)
i18n (0.6.1) i18n (0.6.1)
journey (1.0.4) journey (1.0.4)
jquery-atwho-rails (0.1.7) jquery-atwho-rails (0.3.0)
jquery-rails (2.1.3) jquery-rails (2.1.3)
railties (>= 3.1.0, < 5.0) railties (>= 3.1.0, < 5.0)
thor (~> 0.14) thor (~> 0.14)
...@@ -532,7 +532,7 @@ DEPENDENCIES ...@@ -532,7 +532,7 @@ DEPENDENCIES
guard-spinach guard-spinach
haml-rails haml-rails
httparty httparty
jquery-atwho-rails (= 0.1.7) jquery-atwho-rails (= 0.3.0)
jquery-rails (= 2.1.3) jquery-rails (= 2.1.3)
jquery-turbolinks jquery-turbolinks
jquery-ui-rails (= 2.0.2) jquery-ui-rails (= 2.0.2)
......
...@@ -2,37 +2,55 @@ ...@@ -2,37 +2,55 @@
window.GitLab ?= {} window.GitLab ?= {}
GitLab.GfmAutoComplete = GitLab.GfmAutoComplete =
# private_token: ''
dataSource: ''
# Emoji # Emoji
Emoji: Emoji:
data: [] assetBase: ''
template: '<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>'
# Team Members # Team Members
Members: Members:
data: []
url: ''
params:
private_token: ''
template: '<li data-value="${username}">${username} <small>${name}</small></li>' template: '<li data-value="${username}">${username} <small>${name}</small></li>'
Issues:
template: '<li data-value="${id}"><small>${id}</small> ${title} </li>'
# Add GFM auto-completion to all input fields, that accept GFM input. # Add GFM auto-completion to all input fields, that accept GFM input.
setup: -> setup: ->
input = $('.js-gfm-input') input = $('.js-gfm-input')
# Emoji # Emoji
input.atWho '(?:^|\\s):', input.atwho
data: @Emoji.data at: ':'
tpl: @Emoji.template tpl: @Emoji.template
callbacks:
before_save: (emojis) =>
$.map emojis, (em) => name: em, insert: em+ ':', image: "#{@Emoji.assetBase}/#{em}.png"
# Team Members # Team Members
input.atWho '@', input.atwho
at: '@'
tpl: @Members.template tpl: @Members.template
callback: (query, callback) => search_key: 'search'
request_params = $.extend({}, @Members.params, query: query) callbacks:
$.getJSON(@Members.url, request_params).done (members) => before_save: (members) =>
new_members_data = $.map(members, (m) -> $.map members, (m) => name: m.name, username: m.username, search: "#{m.username} #{m.name}"
username: m.username,
name: m.name input.atwho
) at: '#'
callback(new_members_data) 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
...@@ -93,4 +93,16 @@ class ProjectsController < ProjectResourceController ...@@ -93,4 +93,16 @@ class ProjectsController < ProjectResourceController
format.js format.js
end end
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 end
...@@ -36,5 +36,3 @@ ...@@ -36,5 +36,3 @@
= link_to current_user, class: "profile-pic" do = link_to current_user, class: "profile-pic" do
= image_tag gravatar_icon(current_user.email, 26) = image_tag gravatar_icon(current_user.email, 26)
= render "layouts/init_auto_complete"
:javascript :javascript
$(function() { $(function() {
GitLab.GfmAutoComplete.Members.url = "#{ "/api/v3/projects/#{@project.id}/members" if @project }"; GitLab.GfmAutoComplete.dataSource = "#{autocomplete_sources_project_path(@project)}"
GitLab.GfmAutoComplete.Members.params.private_token = "#{current_user.private_token}"; GitLab.GfmAutoComplete.Emoji.assetBase = '#{image_path("emoji")}'
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.setup(); GitLab.GfmAutoComplete.setup();
}); });
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
= render "layouts/head", title: @project.name_with_namespace = render "layouts/head", title: @project.name_with_namespace
%body{class: "#{app_theme} project", :'data-page' => body_data_page, :'data-project-id' => @project.id } %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/head_panel", title: project_title(@project)
= render "layouts/init_auto_complete"
= render "layouts/flash" = render "layouts/flash"
- if can?(current_user, :download_code, @project) - if can?(current_user, :download_code, @project)
= render 'shared/no_ssh' = render 'shared/no_ssh'
......
...@@ -168,6 +168,7 @@ Gitlab::Application.routes.draw do ...@@ -168,6 +168,7 @@ Gitlab::Application.routes.draw do
member do member do
put :transfer put :transfer
post :fork post :fork
get :autocomplete_sources
end end
resources :blob, only: [:show], constraints: {id: /.+/} resources :blob, only: [:show], constraints: {id: /.+/}
......
...@@ -83,6 +83,10 @@ describe ProjectsController, "routing" do ...@@ -83,6 +83,10 @@ describe ProjectsController, "routing" do
get("/gitlabhq/edit").should route_to('projects#edit', id: 'gitlabhq') get("/gitlabhq/edit").should route_to('projects#edit', id: 'gitlabhq')
end end
it "to #autocomplete_sources" do
get('/gitlabhq/autocomplete_sources').should route_to('projects#autocomplete_sources', id: "gitlabhq")
end
it "to #show" do it "to #show" do
get("/gitlabhq").should route_to('projects#show', id: 'gitlabhq') get("/gitlabhq").should route_to('projects#show', id: 'gitlabhq')
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册