From 8469d18a0e5d2b7177342ca2a2e12ebb21f9af18 Mon Sep 17 00:00:00 2001 From: perploug Date: Fri, 6 Dec 2013 15:09:39 +0100 Subject: [PATCH] fixes search keyboard support --- .../src/common/directives/util.directive.js | 2 +- .../src/common/services/keyboard.service.js | 2 +- .../src/views/common/search.controller.js | 78 ++++++++++++++++++- .../src/views/directives/umb-navigation.html | 9 ++- 4 files changed, 83 insertions(+), 8 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/common/directives/util.directive.js b/src/Umbraco.Web.UI.Client/src/common/directives/util.directive.js index 1005ecd903..ebc4bcea7e 100644 --- a/src/Umbraco.Web.UI.Client/src/common/directives/util.directive.js +++ b/src/Umbraco.Web.UI.Client/src/common/directives/util.directive.js @@ -14,7 +14,7 @@ angular.module('umbraco.directives') .directive('onKeydown', function () { return { link: function (scope, elm, attrs) { - $key('keydown', scope, elm, attrs); + scope.$apply(attrs.onKeydown); } }; }) diff --git a/src/Umbraco.Web.UI.Client/src/common/services/keyboard.service.js b/src/Umbraco.Web.UI.Client/src/common/services/keyboard.service.js index abc606f5c4..0a7be9f5a4 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/keyboard.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/keyboard.service.js @@ -21,7 +21,7 @@ angular.module('umbraco.services') keyboardManagerService.bind = function (label, callback, opt) { //replace ctrl key with meta key - if(isMac){ + if(isMac && label !== "ctrl+space"){ label = label.replace("ctrl","meta"); } diff --git a/src/Umbraco.Web.UI.Client/src/views/common/search.controller.js b/src/Umbraco.Web.UI.Client/src/views/common/search.controller.js index 0f12639fd4..b46616c573 100644 --- a/src/Umbraco.Web.UI.Client/src/views/common/search.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/common/search.controller.js @@ -7,11 +7,83 @@ * Controls the search functionality in the site * */ -function SearchController($scope, searchService, $log, navigationService) { +function SearchController($scope, searchService, $log, $location, navigationService) { $scope.searchTerm = null; $scope.searchResults = []; $scope.isSearching = false; + $scope.selectedResult = -1; + + + $scope.navigateResults = function(ev){ + //38: up 40: down, 13: enter + + switch(ev.keyCode){ + case 38: + iterateResults(true); + break; + case 40: + iterateResults(false); + break; + case 13: + $location.path($scope.selectedItem.editorPath); + navigationService.hideSearch(); + break; + } + }; + + + var group = undefined; + var groupIndex = -1; + var itemIndex = -1; + $scope.selectedItem = undefined; + + + function iterateResults(up){ + //default group + if(!group){ + group = $scope.groups[0]; + groupIndex = 0; + } + + if(up){ + if(itemIndex === 0){ + if(groupIndex === 0){ + gotoGroup($scope.groups.length-1, true); + }else{ + gotoGroup(groupIndex-1, true); + } + }else{ + gotoItem(itemIndex-1); + } + }else{ + if(itemIndex < group.results.length-1){ + gotoItem(itemIndex+1); + }else{ + if(groupIndex === $scope.groups.length-1){ + gotoGroup(0); + }else{ + gotoGroup(groupIndex+1); + } + } + } + } + + function gotoGroup(index, up){ + groupIndex = index; + group = $scope.groups[groupIndex]; + + if(up){ + gotoItem(group.results.length-1); + }else{ + gotoItem(0); + } + } + + function gotoItem(index){ + itemIndex = index; + $scope.selectedItem = group.results[itemIndex]; + } //watch the value change but don't do the search on every change - that's far too many queries // we need to debounce @@ -19,12 +91,14 @@ function SearchController($scope, searchService, $log, navigationService) { if ($scope.searchTerm) { $scope.isSearching = true; navigationService.showSearch(); + $scope.selectedItem = undefined; searchService.searchAll({ term: $scope.searchTerm }).then(function (result) { - $scope.searchResults = result; + $scope.groups = _.filter(result, function(group){return group.results.length > 0;}); }); }else{ $scope.isSearching = false; navigationService.hideSearch(); + $scope.selectedItem = undefined; } }, 100)); diff --git a/src/Umbraco.Web.UI.Client/src/views/directives/umb-navigation.html b/src/Umbraco.Web.UI.Client/src/views/directives/umb-navigation.html index 63c163880c..c686e569e7 100644 --- a/src/Umbraco.Web.UI.Client/src/views/directives/umb-navigation.html +++ b/src/Umbraco.Web.UI.Client/src/views/directives/umb-navigation.html @@ -19,12 +19,13 @@ @@ -38,8 +39,8 @@
Search results
-