From ff1b14d98089e1c33eb6cccaa5a59f0fb8308ebc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 25 Aug 2017 11:23:00 +0200 Subject: [PATCH] Project sharing: Simplified user selection JS code. --- src/templates/projects/sharing.jade | 69 +++++++---------------------- 1 file changed, 16 insertions(+), 53 deletions(-) diff --git a/src/templates/projects/sharing.jade b/src/templates/projects/sharing.jade index 2db20b27..cce206a2 100644 --- a/src/templates/projects/sharing.jade +++ b/src/templates/projects/sharing.jade @@ -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 = $('

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