Refactored user search as JQuery plugin
This commit is contained in:
parent
2bf95223b7
commit
a5d11ec31b
38
src/scripts/tutti/7_user_search.js
Normal file
38
src/scripts/tutti/7_user_search.js
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
(function ( $ ) {
|
||||||
|
// See organizations/view_embed.jade for example use.
|
||||||
|
$.fn.userSearch = function(algolia_application_id, algolia_public_key, algolia_index_users, on_selected) {
|
||||||
|
var client = algoliasearch(algolia_application_id, algolia_public_key);
|
||||||
|
var index = client.initIndex(algolia_index_users);
|
||||||
|
|
||||||
|
var target = this;
|
||||||
|
this.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) {
|
||||||
|
on_selected(event, hit, dataset);
|
||||||
|
})
|
||||||
|
;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
}(jQuery));
|
@ -180,54 +180,29 @@ script.
|
|||||||
}
|
}
|
||||||
|
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
var APPLICATION_ID = '{{config.ALGOLIA_USER}}'
|
$('#user-select').userSearch(
|
||||||
var SEARCH_ONLY_API_KEY = '{{config.ALGOLIA_PUBLIC_KEY}}';
|
'{{config.ALGOLIA_USER}}',
|
||||||
var INDEX_NAME = '{{config.ALGOLIA_INDEX_USERS}}';
|
'{{config.ALGOLIA_PUBLIC_KEY}}',
|
||||||
var client = algoliasearch(APPLICATION_ID, SEARCH_ONLY_API_KEY);
|
'{{config.ALGOLIA_INDEX_USERS}}',
|
||||||
var index = client.initIndex(INDEX_NAME);
|
function (event, hit, dataset) {
|
||||||
|
var $existing = $('li.sharing-users-item[data-user-id="' + hit.objectID + '"]');
|
||||||
$('#user-select').autocomplete({hint: false}, [
|
if ($existing.length) {
|
||||||
{
|
$existing
|
||||||
source: function (q, cb) {
|
.addClass('active')
|
||||||
index.search(q, {hitsPerPage: 5}, function (error, content) {
|
.delay(1000)
|
||||||
if (error) {
|
.queue(function() {
|
||||||
cb([]);
|
$existing.removeClass('active');
|
||||||
return;
|
$existing.dequeue();
|
||||||
}
|
});
|
||||||
cb(content.hits, content);
|
toastr.info('User is already member of this organization');
|
||||||
});
|
}
|
||||||
},
|
else {
|
||||||
displayKey: 'full_name',
|
addUser(hit.objectID);
|
||||||
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 $existing = $('li.sharing-users-item[data-user-id="' + hit.objectID + '"]');
|
|
||||||
if ($existing.length) {
|
|
||||||
$existing
|
|
||||||
.addClass('active')
|
|
||||||
.delay(1000)
|
|
||||||
.queue(function() {
|
|
||||||
console.log('no');
|
|
||||||
$existing.removeClass('active');
|
|
||||||
$existing.dequeue();
|
|
||||||
});
|
|
||||||
toastr.info('User is already member of this organization');
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
addUser('{{ organization["_id"] }}', hit.objectID);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
function addUser(userId) {
|
||||||
function addUser(organizationId, userId){
|
|
||||||
if (!userId || userId.length == 0) {
|
if (!userId || userId.length == 0) {
|
||||||
toastr.error('Please select a user from the list');
|
toastr.error('Please select a user from the list');
|
||||||
return;
|
return;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user