@@ -28,15 +28,39 @@ angular.module('angular-advanced-searchbox', [])
28
28
$scope . searchParams = [ ] ;
29
29
$scope . searchQuery = '' ;
30
30
$scope . setSearchFocus = false ;
31
+ var searchThrottleTimer ;
32
+ var changeBuffer = [ ] ;
31
33
32
- $scope . $watch ( 'searchQuery' , function ( ) {
33
- updateModel ( ) ;
34
- } ) ;
34
+ $scope . $watch ( 'model' , function ( newValue , oldValue ) {
35
+
36
+ if ( angular . equals ( newValue , oldValue ) )
37
+ return ;
35
38
36
- $scope . $watch ( 'searchParams' , function ( ) {
37
- updateModel ( ) ;
39
+ angular . forEach ( $scope . model , function ( value , key ) {
40
+ if ( key === 'query' && $scope . searchQuery !== value ) {
41
+ $scope . searchQuery = value ;
42
+ } else {
43
+ var paramTemplate = $filter ( 'filter' ) ( $scope . parameters , function ( param ) { return param . key === key ; } ) [ 0 ] ;
44
+ var searchParam = $filter ( 'filter' ) ( $scope . searchParams , function ( param ) { return param . key === key ; } ) [ 0 ] ;
45
+
46
+ if ( paramTemplate !== undefined ) {
47
+ if ( searchParam === undefined )
48
+ $scope . addSearchParam ( paramTemplate , value , false ) ;
49
+ else if ( searchParam . value !== value )
50
+ searchParam . value = value ;
51
+ }
52
+ }
53
+ } ) ;
38
54
} , true ) ;
39
55
56
+ $scope . searchParamValueChanged = function ( param ) {
57
+ updateModel ( 'change' , param . key , param . value ) ;
58
+ } ;
59
+
60
+ $scope . searchQueryChanged = function ( query ) {
61
+ updateModel ( 'change' , 'query' , query ) ;
62
+ } ;
63
+
40
64
$scope . enterEditMode = function ( index ) {
41
65
if ( index === undefined )
42
66
return ;
@@ -60,6 +84,7 @@ angular.module('angular-advanced-searchbox', [])
60
84
$scope . typeaheadOnSelect = function ( item , model , label ) {
61
85
$scope . addSearchParam ( item ) ;
62
86
$scope . searchQuery = '' ;
87
+ updateModel ( 'delete' , 'query' ) ;
63
88
} ;
64
89
65
90
$scope . addSearchParam = function ( searchParam , value , enterEditModel ) {
@@ -77,22 +102,29 @@ angular.module('angular-advanced-searchbox', [])
77
102
) ;
78
103
79
104
//TODO: hide used suggestion
105
+
106
+ updateModel ( 'add' , searchParam . key , value ) ;
80
107
} ;
81
108
82
109
$scope . removeSearchParam = function ( index ) {
83
110
if ( index === undefined )
84
111
return ;
85
112
113
+ var searchParam = $scope . searchParams [ index ] ;
86
114
$scope . searchParams . splice ( index , 1 ) ;
87
115
88
116
//TODO: show hidden/removed suggestion
117
+
118
+ updateModel ( 'delete' , searchParam . key ) ;
89
119
} ;
90
120
91
121
$scope . removeAll = function ( ) {
92
122
$scope . searchParams . length = 0 ;
93
123
$scope . searchQuery = '' ;
94
124
95
125
//TODO: show hidden/removed suggestion
126
+
127
+ $scope . model = { } ;
96
128
} ;
97
129
98
130
$scope . editPrevious = function ( currentIndex ) {
@@ -172,21 +204,28 @@ angular.module('angular-advanced-searchbox', [])
172
204
restoreModel ( ) ;
173
205
}
174
206
175
- var searchThrottleTimer ;
176
- function updateModel ( ) {
207
+ function updateModel ( command , key , value ) {
177
208
if ( searchThrottleTimer )
178
209
$timeout . cancel ( searchThrottleTimer ) ;
179
210
180
- searchThrottleTimer = $timeout ( function ( ) {
181
- $scope . model = { } ;
182
-
183
- if ( $scope . searchQuery . length > 0 )
184
- $scope . model . query = $scope . searchQuery ;
211
+ // remove all previous entries to the same search key that was not handled yet
212
+ changeBuffer = $filter ( 'filter' ) ( changeBuffer , function ( change ) { return change . key !== key ; } ) ;
213
+ // add new change to list
214
+ changeBuffer . push ( {
215
+ command : command ,
216
+ key : key ,
217
+ value : value
218
+ } ) ;
185
219
186
- angular . forEach ( $scope . searchParams , function ( param ) {
187
- if ( param . value !== undefined && param . value . length > 0 )
188
- $scope . model [ param . key ] = param . value ;
220
+ searchThrottleTimer = $timeout ( function ( ) {
221
+ angular . forEach ( changeBuffer , function ( change ) {
222
+ if ( change . command === 'delete' )
223
+ delete $scope . model [ change . key ] ;
224
+ else
225
+ $scope . model [ change . key ] = change . value ;
189
226
} ) ;
227
+
228
+ changeBuffer . length = 0 ;
190
229
} , 500 ) ;
191
230
}
192
231
@@ -274,4 +313,4 @@ angular.module('angular-advanced-searchbox', [])
274
313
} ;
275
314
}
276
315
] ) ;
277
- } ) ( ) ;
316
+ } ) ( ) ;
0 commit comments