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() {
|
||||
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();
|
||||
}
|
||||
$('#user-select').userSearch(
|
||||
'{{config.ALGOLIA_USER}}',
|
||||
'{{config.ALGOLIA_PUBLIC_KEY}}',
|
||||
'{{config.ALGOLIA_INDEX_USERS}}',
|
||||
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() {
|
||||
$existing.removeClass('active');
|
||||
$existing.dequeue();
|
||||
});
|
||||
toastr.info('User is already member of this organization');
|
||||
}
|
||||
else {
|
||||
addUser(hit.objectID);
|
||||
}
|
||||
}
|
||||
]).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(organizationId, userId){
|
||||
function addUser(userId) {
|
||||
if (!userId || userId.length == 0) {
|
||||
toastr.error('Please select a user from the list');
|
||||
return;
|
||||
|
Loading…
x
Reference in New Issue
Block a user