Project sharing: Simplified user selection JS code.
This commit is contained in:
@@ -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(src="{{ url_for('static_pillar', filename='assets/js/vendor/jquery.autocomplete-0.22.0.min.js') }}", async=true)
|
||||||
script.
|
script.
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
var APPLICATION_ID = '{{config.ALGOLIA_USER}}'
|
function shareWithUser(event, hit, dataset) {
|
||||||
var SEARCH_ONLY_API_KEY = '{{config.ALGOLIA_PUBLIC_KEY}}';
|
var $found = $('.sharing-users-item[user-id="' + hit.objectID + '"]');
|
||||||
var INDEX_NAME = '{{config.ALGOLIA_INDEX_USERS}}';
|
if ($found.length) {
|
||||||
var client = algoliasearch(APPLICATION_ID, SEARCH_ONLY_API_KEY);
|
$found.addClass('active');
|
||||||
var index = client.initIndex(INDEX_NAME);
|
setTimeout(function(){ $('.sharing-users-item').removeClass('active');}, 350);
|
||||||
|
statusBarSet('info', 'User is already part of the project', 'pi-info');
|
||||||
$('#user-select').autocomplete({hint: false}, [
|
return;
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
addUser(hit.objectID);
|
||||||
|
}
|
||||||
|
|
||||||
|
$('#user-select').userSearch(
|
||||||
|
'{{config.ALGOLIA_USER}}',
|
||||||
|
'{{config.ALGOLIA_PUBLIC_KEY}}',
|
||||||
|
'{{config.ALGOLIA_INDEX_USERS}}',
|
||||||
|
shareWithUser
|
||||||
|
);
|
||||||
});
|
});
|
||||||
function addUser(userId){
|
function addUser(userId){
|
||||||
if (!userId || userId.length == 0) {
|
if (!userId || userId.length == 0) {
|
||||||
|
Reference in New Issue
Block a user