@@ -43,7 +43,7 @@ function PatientSearchWidget(configuration){
4343 var highlightedMouseRowIndex ;
4444 var searchDelayTimer ;
4545 var requestCount = 0 ;
46- var searchUrl = '/' + OPENMRS_CONTEXT_PATH + '/ws/rest/v1/patient' ;
46+ var searchUrl = '/' + OPENMRS_CONTEXT_PATH + '/ws/rest/v1/patientsearch/ patient' ;
4747 var initialData = [ ] ;
4848 var initialPatientData = [ ] ;
4949 var initialPatientUuids = [ ] ;
@@ -140,7 +140,52 @@ function PatientSearchWidget(configuration){
140140 searchOnIdentifierAndName ( query , currRequestCount ) ;
141141 }
142142 else {
143- searchOnExactIdentifierMatchThenIdentifierAndName ( query , currRequestCount , autoSelectIfExactIdentifierMatch ) ;
143+ var gender_search = jq ( '#patient-gender-search' ) . val ( ) ;
144+ var from_search = jq ( '#patient-age-range-from' ) . val ( ) ;
145+ var to_search = jq ( '#patient-age-range-to' ) . val ( ) ;
146+ var date_search ;
147+ if ( jq ( '#patient-birthdate' ) . val ( ) != '' ) {
148+ console . log ( 'birth ' + jq ( '#patient-birthdate' ) . val ( ) ) ;
149+ date_search = new Date ( jq ( '#patient-birthdate' ) . val ( ) ) ;
150+ date_search . setHours ( 0 ) ;
151+ date_search . setMinutes ( 0 ) ;
152+ }
153+
154+
155+ if ( query == '' ) {
156+ if ( gender_search != '' && from_search == '' && to_search == '' && jq ( '#patient-birthdate' ) . val ( ) == '' ) {
157+ searchOnGender ( gender_search , currRequestCount ) ;
158+ } else if ( gender_search == '' && from_search != '' && to_search != '' && jq ( '#patient-birthdate' ) . val ( ) == '' ) {
159+ searchOnRangeOfAge ( from_search , to_search , currRequestCount ) ;
160+ } else if ( gender_search == '' && jq ( '#patient-birthdate' ) . val ( ) != '' ) {
161+ searchOnBirthdate ( date_search . getTime ( ) , currRequestCount ) ;
162+ } else if ( gender_search != '' && from_search != '' && to_search != '' ) {
163+ searchOnGenderAndRangeOfAge ( gender_search , from_search , to_search , currRequestCount ) ;
164+ } else if ( gender_search != '' && jq ( '#patient-birthdate' ) . val ( ) != '' ) {
165+ searchOnGenderAndBirthdate ( gender_search , date_search . getTime ( ) , currRequestCount ) ;
166+ } else {
167+ updateSearchResults ( ) ;
168+ }
169+
170+ }
171+ else {
172+ if ( gender_search != '' ) {
173+ if ( from_search == '' && to_search == '' && jq ( '#patient-birthdate' ) . val ( ) == '' ) {
174+ searchOnIdentifierAndNameAndGender ( query , gender_search , currRequestCount ) ;
175+ } else if ( from_search != '' && to_search != '' ) {
176+ searchOnIdentifierAndNameAndGenderAndRangeOfAge ( query , gender_search , from_search , to_search , currRequestCount ) ;
177+ } else {
178+ searchOnIdentifierAndNameAndGenderAndBirthdate ( query , gender_search , date_search . getTime ( ) , currRequestCount ) ;
179+ }
180+ } else if ( from_search != '' && to_search != '' ) {
181+ searchOnIdentifierAndNameAndRangeOfAge ( query , from_search , to_search , currRequestCount ) ;
182+ } else if ( jq ( '#patient-birthdate' ) . val ( ) != '' ) {
183+ searchOnIdentifierAndNameAndBirthdate ( query , date_search . getTime ( ) , currRequestCount ) ;
184+ } else {
185+ searchOnExactIdentifierMatchThenIdentifierAndName ( query , currRequestCount , autoSelectIfExactIdentifierMatch ) ;
186+ }
187+ }
188+
144189 }
145190 }
146191
@@ -210,6 +255,136 @@ function PatientSearchWidget(configuration){
210255 } ) ;
211256 }
212257
258+ var searchOnIdentifierAndNameAndGender = function ( query , gender_search , currRequestCount ) {
259+ emr . getJSON ( searchUrl , { q : query , gender : gender_search , v : customRep } )
260+ . done ( function ( data ) {
261+ //late ajax responses should be ignored not to overwrite the latest
262+ if ( data && ( ! currRequestCount || currRequestCount >= requestCount ) ) {
263+ updateSearchResults ( data . results ) ;
264+ }
265+ } )
266+ . fail ( function ( jqXHR ) {
267+ failSearch ( ) ;
268+ } ) ;
269+ }
270+
271+ var searchOnIdentifierAndNameAndRangeOfAge = function ( query , from_search , to_search , currRequestCount ) {
272+ emr . getJSON ( searchUrl , { q : query , from : from_search , to : to_search , v : customRep } )
273+ . done ( function ( data ) {
274+ //late ajax responses should be ignored not to overwrite the latest
275+ if ( data && ( ! currRequestCount || currRequestCount >= requestCount ) ) {
276+ updateSearchResults ( data . results ) ;
277+ }
278+ } )
279+ . fail ( function ( jqXHR ) {
280+ failSearch ( ) ;
281+ } ) ;
282+ }
283+
284+ var searchOnIdentifierAndNameAndBirthdate = function ( query , birthdate_search , currRequestCount ) {
285+ emr . getJSON ( searchUrl , { q : query , birthdate : birthdate_search , v : customRep } )
286+ . done ( function ( data ) {
287+ //late ajax responses should be ignored not to overwrite the latest
288+ if ( data && ( ! currRequestCount || currRequestCount >= requestCount ) ) {
289+ updateSearchResults ( data . results ) ;
290+ }
291+ } )
292+ . fail ( function ( jqXHR ) {
293+ failSearch ( ) ;
294+ } ) ;
295+ }
296+
297+ var searchOnIdentifierAndNameAndGenderAndRangeOfAge = function ( query , gender_search , from_search , to_search , currRequestCount ) {
298+ emr . getJSON ( searchUrl , { q : query , gender : gender_search , from : from_search , to : to_search , v : customRep } )
299+ . done ( function ( data ) {
300+ //late ajax responses should be ignored not to overwrite the latest
301+ if ( data && ( ! currRequestCount || currRequestCount >= requestCount ) ) {
302+ updateSearchResults ( data . results ) ;
303+ }
304+ } )
305+ . fail ( function ( jqXHR ) {
306+ failSearch ( ) ;
307+ } ) ;
308+ }
309+
310+ var searchOnIdentifierAndNameAndGenderAndBirthdate = function ( query , gender_search , birthdate_search , currRequestCount ) {
311+ emr . getJSON ( searchUrl , { q : query , gender : gender_search , birthdate : birthdate_search , v : customRep } )
312+ . done ( function ( data ) {
313+ //late ajax responses should be ignored not to overwrite the latest
314+ if ( data && ( ! currRequestCount || currRequestCount >= requestCount ) ) {
315+ updateSearchResults ( data . results ) ;
316+ }
317+ } )
318+ . fail ( function ( jqXHR ) {
319+ failSearch ( ) ;
320+ } ) ;
321+ }
322+
323+ var searchOnGenderAndRangeOfAge = function ( gender_search , from_search , to_search , currRequestCount ) {
324+ emr . getJSON ( searchUrl , { gender : gender_search , from : from_search , to : to_search , v : customRep } )
325+ . done ( function ( data ) {
326+ //late ajax responses should be ignored not to overwrite the latest
327+ if ( data && ( ! currRequestCount || currRequestCount >= requestCount ) ) {
328+ updateSearchResults ( data . results ) ;
329+ }
330+ } )
331+ . fail ( function ( jqXHR ) {
332+ failSearch ( ) ;
333+ } ) ;
334+ }
335+
336+ var searchOnGenderAndBirthdate = function ( gender_search , birthdate_search , currRequestCount ) {
337+ emr . getJSON ( searchUrl , { gender : gender_search , birthdate : birthdate_search , v : customRep } )
338+ . done ( function ( data ) {
339+ //late ajax responses should be ignored not to overwrite the latest
340+ if ( data && ( ! currRequestCount || currRequestCount >= requestCount ) ) {
341+ updateSearchResults ( data . results ) ;
342+ }
343+ } )
344+ . fail ( function ( jqXHR ) {
345+ failSearch ( ) ;
346+ } ) ;
347+ }
348+
349+ var searchOnGender = function ( gender_search , currRequestCount ) {
350+ emr . getJSON ( searchUrl , { gender : gender_search , v : customRep } )
351+ . done ( function ( data ) {
352+ //late ajax responses should be ignored not to overwrite the latest
353+ if ( data && ( ! currRequestCount || currRequestCount >= requestCount ) ) {
354+ updateSearchResults ( data . results ) ;
355+ }
356+ } )
357+ . fail ( function ( jqXHR ) {
358+ failSearch ( ) ;
359+ } ) ;
360+ }
361+
362+ var searchOnRangeOfAge = function ( from_search , to_search , currRequestCount ) {
363+ emr . getJSON ( searchUrl , { from : from_search , to : to_search , v : customRep } )
364+ . done ( function ( data ) {
365+ //late ajax responses should be ignored not to overwrite the latest
366+ if ( data && ( ! currRequestCount || currRequestCount >= requestCount ) ) {
367+ updateSearchResults ( data . results ) ;
368+ }
369+ } )
370+ . fail ( function ( jqXHR ) {
371+ failSearch ( ) ;
372+ } ) ;
373+ }
374+
375+ var searchOnBirthdate = function ( birthdate_search , currRequestCount ) {
376+ emr . getJSON ( searchUrl , { birthdate : birthdate_search , v : customRep } )
377+ . done ( function ( data ) {
378+ //late ajax responses should be ignored not to overwrite the latest
379+ if ( data && ( ! currRequestCount || currRequestCount >= requestCount ) ) {
380+ updateSearchResults ( data . results ) ;
381+ }
382+ } )
383+ . fail ( function ( jqXHR ) {
384+ failSearch ( ) ;
385+ } ) ;
386+ }
387+
213388 var failSearch = function ( ) {
214389 performingSearch = false ;
215390 if ( ! currRequestCount || currRequestCount >= requestCount ) {
@@ -244,6 +419,47 @@ function PatientSearchWidget(configuration){
244419 }
245420 this . reset = reset ;
246421
422+ //Add age range and birthdate search
423+
424+ jq ( '#getAgeAndBirthdateFilter' ) . click ( function ( event ) {
425+ if ( this . checked ) {
426+ jq ( '#patient-search-age-birthdate' ) . css ( 'display' , 'block' ) ;
427+ } else {
428+ jq ( '#patient-search-age-birthdate' ) . css ( 'display' , 'none' ) ;
429+ jq ( '#patient-age' ) . prop ( 'checked' , false ) ;
430+ jq ( '#patient-birthdate' ) . prop ( 'checked' , false ) ;
431+ jq ( '#patient-age-range-from' ) . val ( '' ) ;
432+ jq ( '#patient-age-range-to' ) . val ( '' ) ;
433+ jq ( 'input[type=date]' ) . each ( function resetDate ( ) {
434+ this . value = this . defaultValue ;
435+ } ) ;
436+ }
437+ } )
438+
439+
440+ $ ( "input[name='patient-age-birthdate']" ) . change ( function ( ) {
441+ var ageOrBirthdate = $ ( "input[name='patient-age-birthdate']:checked" ) . val ( )
442+ if ( ageOrBirthdate == "patient-age" ) {
443+ jq ( '#patient-birthdate-search' ) . css ( 'display' , 'none' ) ;
444+ jq ( '#patient-age-range-search' ) . css ( 'display' , 'block' ) ;
445+ jq ( 'input[type=date]' ) . each ( function resetDate ( ) {
446+ this . value = this . defaultValue ;
447+ } ) ;
448+ }
449+ if ( ageOrBirthdate == "patient-birthdate" ) {
450+ jq ( '#patient-age-range-search' ) . css ( 'display' , 'none' ) ;
451+ jq ( '#patient-birthdate-search' ) . css ( 'display' , 'block' ) ;
452+ jq ( '#patient-age-range-from' ) . val ( '' ) ;
453+ jq ( '#patient-age-range-to' ) . val ( '' ) ;
454+ }
455+ } ) ;
456+
457+ //for date type support to browsers
458+ if ( jq ( '[type="date"]' ) . prop ( 'type' ) != 'date' ) {
459+ jq ( '[type="date"]' ) . datepicker ( ) ;
460+ }
461+
462+
247463 var updateSearchResults = function ( results ) {
248464 var dataRows = [ ] ;
249465 if ( results ) {
@@ -295,6 +511,7 @@ function PatientSearchWidget(configuration){
295511 } ) ;
296512 } else if ( config . initialPatients ) {
297513 //show the recently viewed
514+ reset ( ) ;
298515 searchResultsData = initialPatientData ;
299516 dataRows = initialData ;
300517 }
@@ -595,6 +812,39 @@ function PatientSearchWidget(configuration){
595812 clearSearch ( ) ;
596813 } ) ;
597814
815+ var searchByPatientSearchCriteria = function ( ) {
816+ cancelAnyExistingSearch ( ) ;
817+ var text = jq . trim ( input . val ( ) ) ;
818+ var currentCount = ++ requestCount ;
819+ var effectiveSearchDelay = config . searchDelayShort ;
820+ window . setTimeout ( function ( ) {
821+ if ( text == '' || text . length <= config . minSearchCharacters ) {
822+ doSearch ( '' , currentCount ) ;
823+ } else {
824+ doSearch ( text , currentCount ) ;
825+ }
826+ } , effectiveSearchDelay ) ;
827+
828+ }
829+
830+ jq ( 'select' ) . change ( function ( ) {
831+ searchByPatientSearchCriteria ( ) ;
832+ } ) ;
833+
834+ jq ( 'input[type="date"]' ) . change ( function ( ) {
835+ searchByPatientSearchCriteria ( ) ;
836+ } ) ;
837+
838+
839+ jq ( '#patient-age-range-from' ) . keyup ( function ( ) {
840+ searchByPatientSearchCriteria ( ) ;
841+ } ) ;
842+
843+ jq ( '#patient-age-range-to' ) . keyup ( function ( ) {
844+ searchByPatientSearchCriteria ( ) ;
845+ } ) ;
846+
847+
598848 input . keyup ( function ( event ) {
599849 var kc = event . keyCode ;
600850 //ignore enter(because it was handled already onkeydown), keyboard navigation and control keys
0 commit comments