lila/public/javascripts/forum-post.js
2017-08-16 14:41:38 -05:00

86 lines
2.9 KiB
JavaScript

$(function() {
$('#lichess_forum').on('click', 'a.delete', function() {
$.post($(this).attr("href"));
$(this).closest(".post").hide();
return false;
}).on('click', 'form.unsub button', function() {
var $form = $(this).parent().toggleClass('on off');
$.post($form.attr("action") + '?unsub=' + $(this).data('unsub'));
return false;
});
$('.edit.button').add('.edit-post-cancel').click(function(e) {
e.preventDefault();
var post = $(this).closest('.post');
var message = post.find('.message').toggle();
var form = post.find('form.edit-post-form').toggle();
form[0].reset();
form.find('textarea').height(message.height());
});
$('.post-text-area').one('focus', function() {
var textarea = this;
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;
});
};
// 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')
});
var textcomplete = new Textcomplete(new Textcomplete.editors.Textarea(textarea));
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);
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'
});
});
});
});