Merge pull request #1664 from riyad/auto-complete-everywhere
Cleanup auto-completion and add it to all GFM inputs
This commit is contained in:
commit
60b4c88e3a
12 changed files with 89 additions and 66 deletions
57
app/assets/javascripts/gfm_auto_complete.js.coffee
Normal file
57
app/assets/javascripts/gfm_auto_complete.js.coffee
Normal file
|
@ -0,0 +1,57 @@
|
|||
|
||||
###
|
||||
Creates the variables for setting up GFM auto-completion
|
||||
###
|
||||
# Emoji
|
||||
window.autocompleteEmojiData = [];
|
||||
window.autocompleteEmojiTemplate = "<li data-value='${insert}'>${name} <img alt='${name}' height='20' src='${image}' width='20' /></li>";
|
||||
|
||||
# Team Members
|
||||
window.autocompleteMembersUrl = "";
|
||||
window.autocompleteMembersParams =
|
||||
private_token: ""
|
||||
page: 1
|
||||
window.autocompleteMembersData = [];
|
||||
|
||||
|
||||
|
||||
###
|
||||
Add GFM auto-completion to all input fields, that accept GFM input.
|
||||
###
|
||||
window.setupGfmAutoComplete = ->
|
||||
###
|
||||
Emoji
|
||||
###
|
||||
$('.gfm-input').atWho ':',
|
||||
data: autocompleteEmojiData,
|
||||
tpl: autocompleteEmojiTemplate
|
||||
|
||||
###
|
||||
Team Members
|
||||
###
|
||||
$('.gfm-input').atWho '@', (query, callback) ->
|
||||
(getMoreMembers = ->
|
||||
$.getJSON(autocompleteMembersUrl, autocompleteMembersParams)
|
||||
.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
|
||||
|
||||
# show the pop-up with a copy of the current data
|
||||
callback autocompleteMembersData[..]
|
||||
|
||||
# are we past the last page?
|
||||
if newMembersData.length == 0
|
||||
# set static data and stop callbacks
|
||||
$('.gfm-input').atWho '@',
|
||||
data: autocompleteMembersData
|
||||
callback: null
|
||||
else
|
||||
# get next page
|
||||
getMoreMembers()
|
||||
|
||||
# so the next request gets the next page
|
||||
autocompleteMembersParams.page += 1;
|
||||
).call();
|
|
@ -6,6 +6,7 @@ function switchToNewIssue(form){
|
|||
$("#new_issue_dialog").show("fade", { direction: "right" }, 150);
|
||||
$('.top-tabs .add_new').hide();
|
||||
disableButtonIfEmptyField("#issue_title", ".save-btn");
|
||||
setupGfmAutoComplete();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -17,6 +18,7 @@ function switchToEditIssue(form){
|
|||
$("#edit_issue_dialog").show("fade", { direction: "right" }, 150);
|
||||
$('.add_new').hide();
|
||||
disableButtonIfEmptyField("#issue_title", ".save-btn");
|
||||
setupGfmAutoComplete();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -98,6 +98,12 @@ module ApplicationHelper
|
|||
[projects, default_nav, project_nav].flatten.to_json
|
||||
end
|
||||
|
||||
def emoji_autocomplete_source
|
||||
# should be an array of strings
|
||||
# so to_s can be called, because it is sufficient and to_json is too slow
|
||||
Emoji::NAMES.to_s
|
||||
end
|
||||
|
||||
def ldap_enable?
|
||||
Devise.omniauth_providers.include?(:ldap)
|
||||
end
|
||||
|
|
|
@ -14,10 +14,4 @@ module NotesHelper
|
|||
"vote downvote"
|
||||
end
|
||||
end
|
||||
|
||||
def emoji_for_completion
|
||||
# should be an array of strings
|
||||
# so to_s can be called, because it is sufficient and to_json is too slow
|
||||
Emoji::NAMES
|
||||
end
|
||||
end
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
= f.label :title do
|
||||
%strong= "Subject *"
|
||||
.input
|
||||
= f.text_field :title, maxlength: 255, class: "xxlarge"
|
||||
= f.text_field :title, maxlength: 255, class: "xxlarge gfm-input"
|
||||
.issue_middle_block
|
||||
.issue_assignee
|
||||
= f.label :assignee_id do
|
||||
|
@ -37,7 +37,7 @@
|
|||
.clearfix
|
||||
= f.label :description, "Details"
|
||||
.input
|
||||
= f.text_area :description, maxlength: 2000, class: "xxlarge", rows: 14
|
||||
= f.text_area :description, maxlength: 2000, class: "xxlarge gfm-input", rows: 14
|
||||
%p.hint Issues are parsed with #{link_to "GitLab Flavored Markdown", help_markdown_path, target: '_blank'}.
|
||||
|
||||
|
||||
|
|
|
@ -28,6 +28,8 @@
|
|||
My profile
|
||||
= link_to 'Logout', destroy_user_session_path, class: "logout", method: :delete
|
||||
|
||||
= render "layouts/init_auto_complete"
|
||||
|
||||
:javascript
|
||||
$(function(){
|
||||
$("#search").autocomplete({
|
||||
|
|
17
app/views/layouts/_init_auto_complete.html.haml
Normal file
17
app/views/layouts/_init_auto_complete.html.haml
Normal file
|
@ -0,0 +1,17 @@
|
|||
:javascript
|
||||
$(function() {
|
||||
autocompleteMembersUrl = "#{ "/api/v2/projects/#{@project.code}/members" if @project }";
|
||||
autocompleteMembersParams.private_token = "#{current_user.authentication_token}";
|
||||
|
||||
autocompleteEmojiData = #{raw emoji_autocomplete_source};
|
||||
// convert the list so that the items have the right format for completion
|
||||
autocompleteEmojiData = $.map(autocompleteEmojiData, function(value) {
|
||||
return {
|
||||
name: value,
|
||||
insert: value+':',
|
||||
image: '#{image_path("emoji")}/'+value+'.png'
|
||||
}
|
||||
});
|
||||
|
||||
setupGfmAutoComplete();
|
||||
});
|
|
@ -38,7 +38,7 @@
|
|||
.top_box_content
|
||||
= f.label :title do
|
||||
%strong= "Title *"
|
||||
.input= f.text_field :title, class: "input-xxlarge pad", maxlength: 255, rows: 5
|
||||
.input= f.text_field :title, class: "input-xxlarge pad gfm-input", maxlength: 255, rows: 5
|
||||
.middle_box_content
|
||||
= f.label :assignee_id do
|
||||
%i.icon-user
|
||||
|
|
|
@ -36,49 +36,3 @@
|
|||
%a.file_upload.btn.small Upload File
|
||||
= f.file_field :attachment, class: "input-file"
|
||||
%span.hint Any file less than 10 MB
|
||||
|
||||
:javascript
|
||||
$(function(){
|
||||
// init auto-completion of team members
|
||||
var membersUrl = "#{root_url}/api/v2/projects/#{@project.code}/members";
|
||||
var membersParams = {
|
||||
private_token: "#{current_user.authentication_token}",
|
||||
page: 1,
|
||||
};
|
||||
var membersData = [];
|
||||
$('.gfm-input').atWho('@', function(query, callback) {
|
||||
(function getMoreMembers() {
|
||||
$.getJSON(membersUrl, membersParams).
|
||||
success(function(members) {
|
||||
// pick the data we need
|
||||
var newMembersData = $.map(members, function(member) { return member.name });
|
||||
|
||||
// add the new page of data to the rest
|
||||
$.merge(membersData, newMembersData);
|
||||
|
||||
// show the pop-up with a copy of the current data
|
||||
callback(membersData.slice(0));
|
||||
|
||||
// are we past the last page?
|
||||
if (newMembersData.length == 0) {
|
||||
// set static data and stop callbacks
|
||||
$('.gfm-input').atWho('@', { data: membersData, callback: null });
|
||||
} else {
|
||||
// get next page
|
||||
getMoreMembers();
|
||||
}
|
||||
});
|
||||
// next request will get the next page
|
||||
membersParams.page += 1;
|
||||
})();
|
||||
});
|
||||
|
||||
// init auto-completion of emoji
|
||||
var emoji = #{emoji_for_completion};
|
||||
// convert the list so that the items have the right format for completion
|
||||
emoji = $.map(emoji, function(value) {return { key: value+':', name: value }});
|
||||
$('.gfm-input').atWho(':', {
|
||||
data: emoji,
|
||||
tpl: "<li data-value='${key}'>${name} #{escape_javascript image_tag('emoji/${name}.png', :size => '20x20')}</li>"
|
||||
});
|
||||
});
|
||||
|
|
|
@ -10,4 +10,5 @@
|
|||
- else
|
||||
:plain
|
||||
$(".note-form-holder").replaceWith("#{escape_javascript(render 'form')}");
|
||||
setupGfmAutoComplete();
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
|
||||
.bottom_box_content
|
||||
= f.label :content
|
||||
.input= f.text_area :content, class: 'span8'
|
||||
.input= f.text_area :content, class: 'span8 gfm-input'
|
||||
.actions
|
||||
= f.submit 'Save', class: "save-btn btn"
|
||||
= link_to "Cancel", project_wiki_path(@project, :index), class: "btn cancel-btn"
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe NotesHelper do
|
||||
describe "#emoji_for_completion" do
|
||||
it "should be an Array of Strings" do
|
||||
emoji_for_completion.should be_a(Array)
|
||||
emoji_for_completion.each { |emoji| emoji.should be_a(String) }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue