From 0bbd29ac424ca262e2594242b7f87e61e644a42a Mon Sep 17 00:00:00 2001 From: Micheil Smith Date: Wed, 9 Apr 2014 13:25:02 +0100 Subject: [PATCH] Reduce DOM queries for Search elements --- public/src/app.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/public/src/app.js b/public/src/app.js index 108635da2e..af06b1cf80 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -413,21 +413,25 @@ var socket, app.updateHeader = function(err, data) { - $('#search-button').off().on('click', function(e) { + var searchButton = $("#search-button"), + searchFields = $("#search-fields"), + searchInput = $('#search-fields input'); + + function dismissSearch(){ + searchFields.hide(); + searchButton.show(); + } + + searchButton.off().on('click', function(e) { e.stopPropagation(); - $('#search-fields').removeClass('hide').show(); + + searchFields.removeClass('hide').show(); $(this).hide(); - $('#search-fields input').focus(); - $('#search-form').on('submit', function() { - $('#search-fields').hide(); - $('#search-button').show(); - }); + searchInput.focus(); - $('#search-fields input').on('blur', function() { - $('#search-fields').hide(); - $('#search-button').show(); - }); + $('#search-form').on('submit', dismissSearch); + searchInput.on('blur', dismissSearch); return false; }); @@ -442,7 +446,7 @@ var socket, $('#logged-out-menu').addClass('hide'); $('#logged-in-menu').removeClass('hide'); - $('#search-button').removeClass("hide").show(); + searchButton.removeClass("hide").show(); var userLabel = loggedInMenu.find('#user_label'); @@ -462,10 +466,10 @@ var socket, } else { if (allowGuestSearching) { - $('#search-button').removeClass("hide").show(); + searchButton.removeClass("hide").show(); $('#mobile-search-button').removeClass("hide").show(); } else { - $('#search-button').addClass("hide").hide(); + searchButton.addClass("hide").hide(); $('#mobile-search-button').addClass("hide").hide(); }