Project sharing: Simplified user selection JS code.

This commit is contained in:
Sybren A. Stüvel 2017-08-25 11:23:00 +02:00
parent a12838032f
commit ff1b14d980

View File

@ -74,61 +74,24 @@ span#project-edit-title
script(src="{{ url_for('static_pillar', filename='assets/js/vendor/jquery.autocomplete-0.22.0.min.js') }}", async=true)
script.
$(document).ready(function() {
var APPLICATION_ID = '{{config.ALGOLIA_USER}}'
var SEARCH_ONLY_API_KEY = '{{config.ALGOLIA_PUBLIC_KEY}}';
var INDEX_NAME = '{{config.ALGOLIA_INDEX_USERS}}';
var client = algoliasearch(APPLICATION_ID, SEARCH_ONLY_API_KEY);
var index = client.initIndex(INDEX_NAME);
$('#user-select').autocomplete({hint: false}, [
{
source: function (q, cb) {
index.search(q, {hitsPerPage: 5}, function (error, content) {
if (error) {
cb([]);
return;
}
cb(content.hits, content);
});
},
displayKey: 'full_name',
minLength: 2,
limit: 10,
templates: {
suggestion: function (hit) {
var suggestion = hit.full_name + ' (' + hit.username + ')';
var $p = $('<p>').text(suggestion);
return $p.html();
}
}
}
]).on('autocomplete:selected', function (event, hit, dataset) {
var lis = document.getElementsByClassName('sharing-users-item');
var has_match = false;
for (var i = 0; i < lis.length; ++i) {
// Check if the user already is in the list
if ($(lis[i]).attr('user-id') == hit.objectID){
$(lis[i]).addClass('active');
setTimeout(function(){ $('.sharing-users-item').removeClass('active');}, 350);
statusBarSet('info', 'User is already part of the project', 'pi-info');
has_match = false;
break;
} else {
has_match = true;
continue;
}
};
if (has_match){
addUser(hit.objectID);
function shareWithUser(event, hit, dataset) {
var $found = $('.sharing-users-item[user-id="' + hit.objectID + '"]');
if ($found.length) {
$found.addClass('active');
setTimeout(function(){ $('.sharing-users-item').removeClass('active');}, 350);
statusBarSet('info', 'User is already part of the project', 'pi-info');
return;
}
});
addUser(hit.objectID);
}
$('#user-select').userSearch(
'{{config.ALGOLIA_USER}}',
'{{config.ALGOLIA_PUBLIC_KEY}}',
'{{config.ALGOLIA_INDEX_USERS}}',
shareWithUser
);
});
function addUser(userId){
if (!userId || userId.length == 0) {