diff --git a/web/app/assets/javascripts/searchResults.js b/web/app/assets/javascripts/searchResults.js index 32e335ab5..afd4541cc 100644 --- a/web/app/assets/javascripts/searchResults.js +++ b/web/app/assets/javascripts/searchResults.js @@ -55,46 +55,69 @@ return false; } - function onSearchSuccess(response) { + function onSearchSuccess(response) { + context.JK.SearchResultScreen.searchResults(response, false) + } + + context.JK.SearchResultScreen.searchResults = function(response, isSidebar) { var resultCount=0; if (response.search_type === 'musicians') { resultCount = response.musicians.length; // TODO: generalize this for each search result type (band, musician, et. al.) + var selector = isSidebar ? "#template-sidebar-search-result" : "#template-search-musicians-result"; $.each(response.musicians, function(index, val) { // fill in template for Connect pre-click - var template = $('#template-search-musicians-result').html(); - var searchResultHtml = context.JK.fillTemplate(template, { + var template = $(selector).html(); + var args = { userId: val.id, avatar_url: context.JK.resolveAvatarUrl(val.photo_url), profile_url: "/#/profile/" + val.id, userName: val.name, - location: val.location, - instruments: getInstrumentHtml(val.instruments) - }); - - $('#search-results').append(searchResultHtml); + location: val.location + }; + if (!isSidebar) { + args['instruments'] = getInstrumentHtml(val.instruments); + } + selector = isSidebar ? '#sidebar-search-results' : '#search-results'; + $(selector).append(context.JK.fillTemplate(template, args)); // fill in template for Connect post-click - template = $('#template-invitation-sent').html(); + selector = isSidebar ? '#template-sidebar-invitation-sent' : '#template-invitation-sent'; + template = $(selector).html(); var invitationSentHtml = context.JK.fillTemplate(template, { userId: val.id, first_name: val.first_name, profile_url: "/#/profile/" + val.id }); - $('#search-results').append(invitationSentHtml); + selector = isSidebar ? '#sidebar-search-results' : '#search-results'; + $(selector).append(invitationSentHtml); // initialize visibility of the divs - $('div[user-id=' + val.id + '].search-connected').hide(); - $('div[user-id=' + val.id + '].search-result').show(); + if (isSidebar) { + $('div[layout=sidebar user-id=' + val.id + '].sidebar-search-connected').hide(); + $('div[layout=sidebar user-id=' + val.id + '].sidebar-search-result').show(); + } else { + $('div[user-id=' + val.id + '].search-connected').hide(); + $('div[user-id=' + val.id + '].search-result').show(); + } // wire up button click handler if search result is not a friend or the current user - if (!val.is_friend && val.id !== context.JK.currentUserId) { - $('div[user-id=' + val.id + ']').find('.btn-connect-friend').click(sendFriendRequest); - } - else { - $('div[user-id=' + val.id + ']').find('.btn-connect-friend').hide(); - } + if (isSidebar) { + var $sidebar = $('div[layout=sidebar] div[user-id=' + val.id + ']'); + if (!val.is_friend && val.id !== context.JK.currentUserId) { + $sidebar.find('.btn-connect-friend').click(sendFriendRequest); + } else { + // hide the button if the search result is already a friend + $sidebar.find('.btn-connect-friend').hide(); + } + } else { + if (!val.is_friend && val.id !== context.JK.currentUserId) { + $('div[user-id=' + val.id + ']').find('.btn-connect-friend').click(sendFriendRequest); + } else { + $('div[user-id=' + val.id + ']').find('.btn-connect-friend').hide(); + } + } }); } else if (response.search_type === 'bands') { } else if (response.search_type === 'fans') { @@ -114,26 +137,45 @@ $('div[user-id=' + val.id + '].search-result').show(); }); } + if (isSidebar) { + // show header + $('#sidebar-search-header').show(); + // hide panels + $('[layout-panel="contents"]').hide(); + $('[layout-panel="contents"]').css({"height": "1px"}); + // resize search results area + $('#sidebar-search-results').height(context.JK.Sidebar.getHeight() + 'px'); + } else { + $('#result-count').html(resultCount); + if (resultCount === 1) { + $('#result-count').append(" Result for: "); + } else { + $('#result-count').append(" Results for: "); + } + } + }; - $('#result-count').html(resultCount); - if (resultCount === 1) { - $('#result-count').append(" Result for: "); - } else { - $('#result-count').append(" Results for: "); - } - } + function friendRequestCallbackSidebar(userId) { + // toggle the pre-click and post-click divs + $('div[layout=sidebar] div[user-id=' + userId + '].sidebar-search-connected').show(); + $('div[layout=sidebar] div[user-id=' + userId + '].sidebar-search-result').hide(); + } - function friendRequestCallback(userId) { - // toggle the pre-click and post-click divs - $('div[user-id=' + userId + '].search-connected').show(); - $('div[user-id=' + userId + '].search-result').hide(); - } + function friendRequestCallbackSearchResults(userId) { + // toggle the pre-click and post-click divs + $('div[user-id=' + userId + '].search-connected').show(); + $('div[user-id=' + userId + '].search-result').hide(); + } - function sendFriendRequest(evt) { + function sendFriendRequest(evt) { evt.stopPropagation(); var userId = $(this).parent().attr('user-id'); - context.JK.sendFriendRequest(app, userId, friendRequestCallback); - } + if ($(this).closest('#sidebar-search-results')) { + context.JK.sendFriendRequest(app, userId, friendRequestCallbackSidebar); + } else { + context.JK.sendFriendRequest(app, userId, friendRequestCallbackSearchResults); + } + } function getInstrumentHtml(instruments) { var instrumentLogoHtml = ''; diff --git a/web/app/assets/javascripts/sidebar.js b/web/app/assets/javascripts/sidebar.js index 21ca0f8d2..7500adce2 100644 --- a/web/app/assets/javascripts/sidebar.js +++ b/web/app/assets/javascripts/sidebar.js @@ -258,7 +258,6 @@ } function search(query) { - logger.debug('query=' + query); if (query !== '') { context.JK.search(query, app, onSearchSuccess); @@ -266,58 +265,10 @@ } function onSearchSuccess(response) { - logger.debug(response); - // TODO: generalize this for each search result type (band, musician, recordings, et. al.) - $.each(response.musicians, function(index, val) { - // fill in template for Connect pre-click - var template = $('#template-sidebar-search-result').html(); - var searchResultHtml = context.JK.fillTemplate(template, { - userId: val.id, - avatar_url: context.JK.resolveAvatarUrl(val.photo_url), - profile_url: "/#/profile/" + val.id, - userName: val.name, - location: val.location - }); - - $('#sidebar-search-results').append(searchResultHtml); - - // fill in template for Connect post-click - template = $('#template-sidebar-invitation-sent').html(); - var invitationSentHtml = context.JK.fillTemplate(template, { - userId: val.id, - first_name: val.first_name, - profile_url: "/#/profile/" + val.id - }); - - $('#sidebar-search-results').append(invitationSentHtml); - - // initialize visibility of the divs - $('div[layout=sidebar] div[user-id=' + val.id + '].sidebar-search-connected').hide(); - $('div[layout=sidebar] div[user-id=' + val.id + '].sidebar-search-result').show(); - - // wire up button click handler if search result is not a friend or the current user - var $sidebar = $('div[layout=sidebar] div[user-id=' + val.id + ']'); - if (!val.is_friend && val.id !== context.JK.currentUserId) { - $sidebar.find('.btn-connect-friend').click(sendFriendRequest); - } - // hide the button if the search result is already a friend - else { - $sidebar.find('.btn-connect-friend').hide(); - } - }); - - // show header - $('#sidebar-search-header').show(); - - // hide panels - $('[layout-panel="contents"]').hide(); - $('[layout-panel="contents"]').css({"height": "1px"}); - - // resize search results area - $('#sidebar-search-results').height(getHeight() + 'px'); + context.JK.SearchResultScreen.searchResults(response, true) } - function getHeight() { + context.JK.Sidebar.getHeight = function() { // TODO: refactor this - copied from layout.js var sidebarHeight = $(context).height() - 75 - 2 * 60 + $('[layout-sidebar-expander]').height(); var combinedHeaderHeight = $('[layout-panel="contents"]').length * 36; @@ -330,7 +281,7 @@ function showFriendsPanel() { var $expandedPanelContents = $('[layout-id="panelFriends"] [layout-panel="contents"]'); - var expandedPanelHeight = getHeight(); + var expandedPanelHeight = context.JK.Sidebar.getHeight(); // hide all other contents $('[layout-panel="contents"]').hide(); @@ -341,18 +292,6 @@ $expandedPanelContents.animate({"height": expandedPanelHeight + "px"}, 400); } - function friendRequestCallback(userId) { - // toggle the pre-click and post-click divs - $('div[layout=sidebar] div[user-id=' + userId + '].sidebar-search-connected').show(); - $('div[layout=sidebar] div[user-id=' + userId + '].sidebar-search-result').hide(); - } - - function sendFriendRequest(evt) { - evt.stopPropagation(); - var userId = $(this).parent().attr('user-id'); - context.JK.sendFriendRequest(app, userId, friendRequestCallback); - } - function hideSearchResults() { emptySearchResults(); $('#search-input').val('');