Skip to content

Commit 7f0c11a

Browse files
committed
bump latest changes, set version to 2.0.0-dev
1 parent 8442b34 commit 7f0c11a

8 files changed

+156
-54
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-advanced-searchbox",
3-
"version": "1.1.1",
3+
"version": "2.0.0-dev",
44
"homepage": "https://github.com/dnauck/angular-advanced-searchbox",
55
"authors": [
66
"Daniel Nauck <d.nauck@nauck-it.de>"

dist/angular-advanced-searchbox-tpls.js

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,60 @@ angular.module('angular-advanced-searchbox', [])
1616
restrict: 'E',
1717
scope: {
1818
model: '=ngModel',
19-
parameters: '='
19+
parameters: '=',
20+
placeholder: '@'
2021
},
2122
replace: true,
2223
templateUrl: 'angular-advanced-searchbox.html',
2324
controller: [
2425
'$scope', '$attrs', '$element', '$timeout', '$filter',
2526
function ($scope, $attrs, $element, $timeout, $filter) {
2627

27-
$scope.placeholder = $attrs.placeholder || 'Search ...';
28+
$scope.placeholder = $scope.placeholder || 'Search ...';
2829
$scope.searchParams = [];
2930
$scope.searchQuery = '';
3031
$scope.setSearchFocus = false;
32+
var searchThrottleTimer;
33+
var changeBuffer = [];
3134

32-
$scope.$watch('searchQuery', function () {
33-
updateModel();
34-
});
35+
$scope.$watch('model', function (newValue, oldValue) {
36+
37+
if(angular.equals(newValue, oldValue))
38+
return;
3539

36-
$scope.$watch('searchParams', function () {
37-
updateModel();
40+
angular.forEach($scope.model, function (value, key) {
41+
if (key === 'query' && $scope.searchQuery !== value) {
42+
$scope.searchQuery = value;
43+
} else {
44+
var paramTemplate = $filter('filter')($scope.parameters, function (param) { return param.key === key; })[0];
45+
var searchParam = $filter('filter')($scope.searchParams, function (param) { return param.key === key; })[0];
46+
47+
if (paramTemplate !== undefined) {
48+
if(searchParam === undefined)
49+
$scope.addSearchParam(paramTemplate, value, false);
50+
else if (searchParam.value !== value )
51+
searchParam.value = value;
52+
}
53+
}
54+
});
55+
56+
// delete not existing search parameters from internal state array
57+
angular.forEach($scope.searchParams, function (value, key){
58+
if (!$scope.model.hasOwnProperty(value.key)){
59+
var index = $scope.searchParams.map(function(e) { return e.key; }).indexOf(value.key);
60+
$scope.removeSearchParam(index);
61+
}
62+
});
3863
}, true);
3964

65+
$scope.searchParamValueChanged = function (param) {
66+
updateModel('change', param.key, param.value);
67+
};
68+
69+
$scope.searchQueryChanged = function (query) {
70+
updateModel('change', 'query', query);
71+
};
72+
4073
$scope.enterEditMode = function(index) {
4174
if (index === undefined)
4275
return;
@@ -60,12 +93,20 @@ angular.module('angular-advanced-searchbox', [])
6093
$scope.typeaheadOnSelect = function (item, model, label) {
6194
$scope.addSearchParam(item);
6295
$scope.searchQuery = '';
96+
updateModel('delete', 'query');
97+
};
98+
99+
$scope.isUnsedParameter = function (value, index) {
100+
return $filter('filter')($scope.searchParams, function (param) { return param.key === value.key; }).length === 0;
63101
};
64102

65103
$scope.addSearchParam = function (searchParam, value, enterEditModel) {
66104
if (enterEditModel === undefined)
67105
enterEditModel = true;
68106

107+
if (!$scope.isUnsedParameter(searchParam))
108+
return;
109+
69110
$scope.searchParams.push(
70111
{
71112
key: searchParam.key,
@@ -76,23 +117,24 @@ angular.module('angular-advanced-searchbox', [])
76117
}
77118
);
78119

79-
//TODO: hide used suggestion
120+
updateModel('add', searchParam.key, value);
80121
};
81122

82123
$scope.removeSearchParam = function (index) {
83124
if (index === undefined)
84125
return;
85126

127+
var searchParam = $scope.searchParams[index];
86128
$scope.searchParams.splice(index, 1);
87129

88-
//TODO: show hidden/removed suggestion
130+
updateModel('delete', searchParam.key);
89131
};
90132

91133
$scope.removeAll = function() {
92134
$scope.searchParams.length = 0;
93135
$scope.searchQuery = '';
94136

95-
//TODO: show hidden/removed suggestion
137+
$scope.model = {};
96138
};
97139

98140
$scope.editPrevious = function(currentIndex) {
@@ -172,21 +214,28 @@ angular.module('angular-advanced-searchbox', [])
172214
restoreModel();
173215
}
174216

175-
var searchThrottleTimer;
176-
function updateModel() {
217+
function updateModel(command, key, value) {
177218
if (searchThrottleTimer)
178219
$timeout.cancel(searchThrottleTimer);
179220

180-
searchThrottleTimer = $timeout(function () {
181-
$scope.model = {};
182-
183-
if ($scope.searchQuery.length > 0)
184-
$scope.model.query = $scope.searchQuery;
221+
// remove all previous entries to the same search key that was not handled yet
222+
changeBuffer = $filter('filter')(changeBuffer, function (change) { return change.key !== key; });
223+
// add new change to list
224+
changeBuffer.push({
225+
command: command,
226+
key: key,
227+
value: value
228+
});
185229

186-
angular.forEach($scope.searchParams, function (param) {
187-
if (param.value !== undefined && param.value.length > 0)
188-
$scope.model[param.key] = param.value;
230+
searchThrottleTimer = $timeout(function () {
231+
angular.forEach(changeBuffer, function (change) {
232+
if(change.command === 'delete')
233+
delete $scope.model[change.key];
234+
else
235+
$scope.model[change.key] = change.value;
189236
});
237+
238+
changeBuffer.length = 0;
190239
}, 500);
191240
}
192241

@@ -275,11 +324,12 @@ angular.module('angular-advanced-searchbox', [])
275324
}
276325
]);
277326
})();
327+
278328
angular.module('angular-advanced-searchbox').run(['$templateCache', function($templateCache) {
279329
'use strict';
280330

281331
$templateCache.put('angular-advanced-searchbox.html',
282-
"<div class=advancedSearchBox ng-class={active:focus} ng-init=\"focus = false\"><span ng-show=\"searchParams.length < 1 && searchQuery.length === 0\" class=\"search-icon glyphicon glyphicon-search\"></span> <a ng-href=\"\" ng-show=\"searchParams.length > 0 || searchQuery.length > 0\" ng-click=removeAll() role=button><span class=\"remove-all-icon glyphicon glyphicon-trash\"></span></a><div><div class=search-parameter ng-repeat=\"searchParam in searchParams\"><a ng-href=\"\" ng-click=removeSearchParam($index) role=button><span class=\"remove glyphicon glyphicon-trash\"></span></a><div class=key>{{searchParam.name}}:</div><div class=value><span ng-show=!searchParam.editMode ng-click=enterEditMode($index)>{{searchParam.value}}</span> <input name=value nit-auto-size-input nit-set-focus=searchParam.editMode ng-keydown=\"keydown($event, $index)\" ng-blur=leaveEditMode($index) ng-show=searchParam.editMode ng-model=searchParam.value placeholder=\"{{searchParam.placeholder}}\"></div></div><input name=searchbox class=search-parameter-input nit-set-focus=setSearchFocus ng-keydown=keydown($event) placeholder={{placeholder}} ng-focus=\"focus = true\" ng-blur=\"focus = false\" typeahead-on-select=\"typeaheadOnSelect($item, $model, $label)\" typeahead=\"parameter as parameter.name for parameter in parameters | filter:{name:$viewValue} | limitTo:8\" ng-model=\"searchQuery\"></div><div class=search-parameter-suggestions ng-show=\"parameters && focus\"><span class=title>Parameter Suggestions:</span> <span class=search-parameter ng-repeat=\"param in parameters | limitTo:8\" ng-mousedown=addSearchParam(param)>{{param.name}}</span></div></div>"
332+
"<div class=advancedSearchBox ng-class={active:focus} ng-init=\"focus = false\" ng-click=\"!focus ? setSearchFocus = true : null\"><span ng-show=\"searchParams.length < 1 && searchQuery.length === 0\" class=\"search-icon glyphicon glyphicon-search\"></span> <a ng-href=\"\" ng-show=\"searchParams.length > 0 || searchQuery.length > 0\" ng-click=removeAll() role=button><span class=\"remove-all-icon glyphicon glyphicon-trash\"></span></a><div><div class=search-parameter ng-repeat=\"searchParam in searchParams\"><a ng-href=\"\" ng-click=removeSearchParam($index) role=button><span class=\"remove glyphicon glyphicon-trash\"></span></a><div class=key>{{searchParam.name}}:</div><div class=value><span ng-if=!searchParam.editMode ng-click=enterEditMode($index)>{{searchParam.value}}</span> <input name=value nit-auto-size-input nit-set-focus=searchParam.editMode ng-keydown=\"keydown($event, $index)\" ng-blur=leaveEditMode($index) ng-if=searchParam.editMode ng-change=searchParamValueChanged(searchParam) ng-model=searchParam.value placeholder=\"{{searchParam.placeholder}}\"></div></div><input name=searchbox class=search-parameter-input nit-auto-size-input nit-set-focus=setSearchFocus ng-keydown=keydown($event) placeholder={{placeholder}} ng-focus=\"focus = true\" ng-blur=\"focus = false\" typeahead-on-select=\"typeaheadOnSelect($item, $model, $label)\" typeahead=\"parameter as parameter.name for parameter in parameters | filter:isUnsedParameter | filter:{name:$viewValue} | limitTo:8\" ng-change=searchQueryChanged(searchQuery) ng-model=\"searchQuery\"></div><div class=search-parameter-suggestions ng-show=\"parameters && focus\"><span class=title>Parameter Suggestions:</span> <span class=search-parameter ng-repeat=\"param in parameters | filter:isUnsedParameter | limitTo:8\" ng-mousedown=addSearchParam(param)>{{param.name}}</span></div></div>"
283333
);
284334

285335
}]);

0 commit comments

Comments
 (0)