From a5d11ec31bd45bc5268521df095ed43cf22a1a05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Wed, 23 Aug 2017 17:12:58 +0200 Subject: [PATCH] Refactored user search as JQuery plugin --- src/scripts/tutti/7_user_search.js | 38 ++++++++++++ src/templates/organizations/view_embed.jade | 65 +++++++-------------- 2 files changed, 58 insertions(+), 45 deletions(-) create mode 100644 src/scripts/tutti/7_user_search.js diff --git a/src/scripts/tutti/7_user_search.js b/src/scripts/tutti/7_user_search.js new file mode 100644 index 00000000..8a45e428 --- /dev/null +++ b/src/scripts/tutti/7_user_search.js @@ -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 = $('

').text(suggestion); + return $p.html(); + } + } + } + ]) + .on('autocomplete:selected', function (event, hit, dataset) { + on_selected(event, hit, dataset); + }) + ; + + return this; + }; +}(jQuery)); diff --git a/src/templates/organizations/view_embed.jade b/src/templates/organizations/view_embed.jade index 7042514a..ebfe615b 100644 --- a/src/templates/organizations/view_embed.jade +++ b/src/templates/organizations/view_embed.jade @@ -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 = $('

').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;