From 70e9dd1712f3b63961ba101f624f8d1a1aaa5150 Mon Sep 17 00:00:00 2001 From: Dirk Groenen Date: Sun, 27 Dec 2015 16:26:50 +0100 Subject: [PATCH] Keep focus on field while typing, fixes #167 --- src/app/search/search.controller.js | 32 ++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/app/search/search.controller.js b/src/app/search/search.controller.js index a34898c4..1281df81 100644 --- a/src/app/search/search.controller.js +++ b/src/app/search/search.controller.js @@ -29,11 +29,14 @@ angular.module('mopify.search', [ .controller("SearchController", function SearchController($rootScope, $scope, $routeParams, $route, $timeout, $location, Spotify, SpotifyLogin, mopidyservice, stationservice, util, Settings){ - $scope.query = $routeParams.query; - var typingTimeout = null; + $scope.$watch(function() { + return $routeParams.query; + }, function(val) { + $scope.query = val; + $scope.typing(); + }); - // Set focus on input - $rootScope.focussearch = true; + var typingTimeout = null; // Define empty result scope $scope.results = { @@ -64,7 +67,7 @@ angular.module('mopify.search', [ */ $scope.typing = function(event){ // Close the search overlay on ESC press - if(event.keyCode === 27) + if(event !== undefined && event.keyCode === 27) $scope.closeSearch(); if($scope.query.trim().length === 0 || $scope.query === previousQuery) @@ -134,6 +137,9 @@ angular.module('mopify.search', [ resultsloaded++; if(resultsloaded == 2) getTopMatchingResult($scope.query, $scope.results); + + // Put focus on search + $rootScope.focussearch = true; }); }; @@ -294,8 +300,14 @@ angular.module('mopify.search', [ return; if($scope.query.trim().length > 0 && $scope.query !== previous){ - $location.url("/search?query=" + $scope.query + "&refer=" + $location.url()); - $scope.query = ""; + var refer; + + if($location.url().indexOf("/search") > -1) + refer = $routeParams.refer; + else + refer = $location.url(); + + $location.url("/search?query=" + $scope.query + "&refer=" + refer); } previous = $scope.query; @@ -313,5 +325,11 @@ angular.module('mopify.search', [ } }); + $scope.$watch(function() { + return $routeParams.query; + }, function(val) { + $scope.query = val; + }); + });