fix forum mention autocomplete

This commit is contained in:
Thibault Duplessis 2017-08-16 14:41:38 -05:00
parent 7d1fbd1a11
commit 40e2dfbb18
3 changed files with 55 additions and 59 deletions

View file

@ -21,72 +21,65 @@ $(function() {
form.find('textarea').height(message.height());
});
lichess.loadScript('/assets/vendor/jquery-textcomplete/dist/jquery.textcomplete.js').then(function() {
$('.post-text-area').one('focus', function() {
var getThreadParticipants = function() {
var topicId = $('.post-text-area').attr('data-topic');
var textarea = this;
if (!topicId) {
return jQuery.Deferred().resolve([]);
} else {
return $.ajax({
url: "/forum/participants/" + topicId
lichess.loadScript('/assets/vendor/textcomplete.min.js').then(function() {
var searchCandidates = function(term, candidateUsers) {
return candidateUsers.filter(function(user) {
return user.toLowerCase().indexOf(term.toLowerCase()) === 0;
});
}
};
};
var searchCandidates = function(term, candidateUsers) {
return candidateUsers.filter(function(user) {
return user.toLowerCase().indexOf(term.toLowerCase()) === 0;
// We only ask the server for the thread participants once the user has clicked the text box as most hits to the
// forums will be only to read the thread. So the 'thread participants' starts out empty until the post text area
// is focused.
var threadParticipants = $.ajax({
url: "/forum/participants/" + $('.post-text-area').attr('data-topic')
});
};
// We only ask the server for the thread participants once the user has clicked the text box as most hits to the
// forums will be only to read the thread. So the 'thread participants' starts out empty until the post text area
// is focused.
var threadParticipants = Promise.resolve([]);
var textcomplete = new Textcomplete(new Textcomplete.editors.Textarea(textarea));
$('.post-text-area').textcomplete([{
match: /(^|\s)@(|[a-zA-Z_-][\w-]{0,19})$/,
search: function(term, callback) {
textcomplete.register([{
match: /(^|\s)@(|[a-zA-Z_-][\w-]{0,19})$/,
search: function(term, callback) {
// Initially we only autocomplete by participants in the thread. As the user types more,
// we can autocomplete against all users on the site.
threadParticipants.then(function(participants) {
var forumParticipantCandidates = searchCandidates(term, participants);
// Initially we only autocomplete by participants in the thread. As the user types more,
// we can autocomplete against all users on the site.
threadParticipants.then(function(participants) {
var forumParticipantCandidates = searchCandidates(term, participants);
if (forumParticipantCandidates.length != 0) {
// We always prefer a match on the forum thread partcipants' usernames
callback(forumParticipantCandidates);
}
else if (term.length >= 3) {
// We fall back to every site user after 3 letters of the username have been entered
// and there are no matches in the forum thread participants
$.ajax({
url: "/player/autocomplete",
data: {
term: term
},
success: function(candidateUsers) {
callback(searchCandidates(term, candidateUsers));
},
cache: true
});
} else {
callback([]);
}
});
},
replace: function(mention) {
return '$1@' + mention + ' ';
}
}], {
placement: 'top',
appendTo: '#lichess_forum'
});
$('.post-text-area').one('focus', function() {
threadParticipants = Promise.resolve(getThreadParticipants());
if (forumParticipantCandidates.length != 0) {
// We always prefer a match on the forum thread partcipants' usernames
callback(forumParticipantCandidates);
}
else if (term.length >= 3) {
// We fall back to every site user after 3 letters of the username have been entered
// and there are no matches in the forum thread participants
$.ajax({
url: "/player/autocomplete",
data: {
term: term
},
success: function(candidateUsers) {
callback(searchCandidates(term, candidateUsers));
},
cache: true
});
} else {
callback([]);
}
});
},
replace: function(mention) {
return '$1@' + mention + ' ';
}
}], {
placement: 'top',
appendTo: '#lichess_forum'
});
});
});
});

View file

@ -316,8 +316,9 @@ div.bar div.pagination {
font-weight: bold;
cursor: pointer;
}
.textcomplete-dropdown li:first-child {
border-top: none;
.textcomplete-dropdown li.textcomplete-header,
.textcomplete-dropdown li.textcomplete-footer {
display: none;
}
.textcomplete-dropdown li:hover,
.textcomplete-dropdown .active {

2
public/vendor/textcomplete.min.js vendored Normal file

File diff suppressed because one or more lines are too long