Skip to content

Commit 3242b4e

Browse files
committed
refactored focus setting, finally fixed #50
1 parent 8164ed2 commit 3242b4e

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/angular-advanced-searchbox.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="advancedSearchBox" ng-class="{active:focus}" ng-init="focus = false" ng-click="!focus ? setSearchFocus = true : null">
1+
<div class="advancedSearchBox" ng-class="{active:focus}" ng-init="focus = false" ng-click="!focus ? setFocusFor('searchbox') : null">
22
<span ng-show="searchParams.length < 1 && searchQuery.length === 0" class="search-icon glyphicon glyphicon-search"></span>
33
<a ng-href="" ng-show="searchParams.length > 0 || searchQuery.length > 0" ng-click="removeAll()" role="button">
44
<span class="remove-all-icon glyphicon glyphicon-trash"></span>
@@ -14,7 +14,7 @@
1414
<input name="value"
1515
type="{{searchParam.type}}"
1616
nit-auto-size-input
17-
nit-set-focus="searchParam.editMode"
17+
set-focus-on="{{'searchParam:' + searchParam.key}}"
1818
ng-keydown="keydown($event, $index)"
1919
ng-blur="leaveEditMode($event, $index)"
2020
ng-show="searchParam.editMode"
@@ -33,7 +33,7 @@
3333
class="search-parameter-input"
3434
type="text"
3535
nit-auto-size-input
36-
nit-set-focus="setSearchFocus"
36+
set-focus-on="searchbox"
3737
ng-keydown="keydown($event)"
3838
placeholder="{{placeholder}}"
3939
ng-focus="focus = true"

src/angular-advanced-searchbox.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ angular.module('angular-advanced-searchbox', [])
2727
return attr.templateUrl || 'angular-advanced-searchbox.html';
2828
},
2929
controller: [
30-
'$scope', '$attrs', '$element', '$timeout', '$filter',
31-
function ($scope, $attrs, $element, $timeout, $filter) {
30+
'$scope', '$attrs', '$element', '$timeout', '$filter', 'setFocusFor',
31+
function ($scope, $attrs, $element, $timeout, $filter, setFocusFor) {
3232

3333
$scope.parametersLabel = $scope.parametersLabel || 'Parameter Suggestions';
3434
$scope.parametersDisplayLimit = $scope.parametersDisplayLimit || 8;
3535
$scope.placeholder = $scope.placeholder || 'Search ...';
3636
$scope.searchThrottleTime = $scope.searchThrottleTime || 1000;
3737
$scope.searchParams = [];
3838
$scope.searchQuery = '';
39-
$scope.setSearchFocus = false;
39+
$scope.setFocusFor = setFocusFor;
4040
var searchThrottleTimer;
4141
var changeBuffer = [];
4242

@@ -116,6 +116,7 @@ angular.module('angular-advanced-searchbox', [])
116116

117117
var searchParam = $scope.searchParams[index];
118118
searchParam.editMode = true;
119+
setFocusFor('searchParam:' + searchParam.key);
119120

120121
$scope.$emit('advanced-searchbox:enteredEditMode', searchParam);
121122
};
@@ -223,7 +224,7 @@ angular.module('angular-advanced-searchbox', [])
223224
$scope.enterEditMode(undefined, $scope.searchParams.length - 1);
224225
} else if ($scope.searchParams.length === 0) {
225226
// no search parameter available anymore
226-
$scope.setSearchFocus = true;
227+
setFocusFor('searchbox');
227228
}
228229
};
229230

@@ -237,7 +238,7 @@ angular.module('angular-advanced-searchbox', [])
237238
if (currentIndex < $scope.searchParams.length - 1) {
238239
$scope.enterEditMode(undefined, currentIndex + 1);
239240
} else {
240-
$scope.setSearchFocus = true;
241+
setFocusFor('searchbox');
241242
}
242243
};
243244

@@ -363,27 +364,30 @@ angular.module('angular-advanced-searchbox', [])
363364
]
364365
};
365366
})
366-
.directive('nitSetFocus', [
367-
'$timeout', '$parse',
368-
function($timeout, $parse) {
367+
.directive('setFocusOn', [
368+
function() {
369369
return {
370370
restrict: 'A',
371371
link: function($scope, $element, $attrs) {
372-
var model = $parse($attrs.nitSetFocus);
373-
$scope.$watch(model, function(value) {
374-
if (value === true) {
375-
$timeout(function() {
376-
$element[0].focus();
377-
});
372+
return $scope.$on('advanced-searchbox:setFocusOn', function(e, id) {
373+
if (id === $attrs.setFocusOn) {
374+
return $element[0].focus();
378375
}
379376
});
380-
/*$element.bind('blur', function() {
381-
$scope.$apply(model.assign($scope, false));
382-
});*/
383377
}
384378
};
385379
}
386380
])
381+
.factory('setFocusFor', [
382+
'$rootScope', '$timeout',
383+
function($rootScope, $timeout) {
384+
return function(id) {
385+
return $timeout(function() {
386+
return $rootScope.$broadcast('advanced-searchbox:setFocusOn', id);
387+
});
388+
};
389+
}
390+
])
387391
.directive('nitAutoSizeInput', [
388392
'$timeout',
389393
function($timeout) {

0 commit comments

Comments
 (0)