@@ -2,32 +2,42 @@ $(document).ready(function(){
2
2
let timer ;
3
3
4
4
$ ( ".dn_link_container input[type=text]" ) . on ( "keyup" , function ( event ) {
5
- // Remove value if field is emptied
6
- if ( $ ( this ) . val ( ) . length == 0 ) {
5
+ // Remove value if field is emptied or less than minimal characters
6
+ if ( $ ( this ) . val ( ) . length <= 2 ) {
7
7
$ ( this ) . siblings ( 'input[type=hidden]' ) . val ( '' ) ;
8
+ $ ( this ) . siblings ( 'div.dn_link_suggestions' ) . empty ( ) ;
8
9
}
9
10
// Minimal search characters
10
11
if ( $ ( this ) . val ( ) . length > 2 ) {
11
12
if ( timer ) {
12
13
clearTimeout ( timer ) ;
13
- // clear any existing list
14
14
$ ( this ) . siblings ( 'div.dn_link_suggestions' ) . empty ( ) ;
15
15
}
16
16
17
17
timer = setTimeout ( ( ) => {
18
18
$ . post ( "index.php" , { 'apiendpoint' : 'search_dn' , 'search' : $ ( this ) . val ( ) } , ( data ) => {
19
19
// clear existing list
20
20
$ ( this ) . siblings ( 'div.dn_link_suggestions' ) . empty ( ) ;
21
- // add entries to list
22
- data . forEach ( ( entry ) => {
23
- const $elem = $ ( `<button type="button" class="list-group-item list-group-item-action">${ entry . display } </button>` ) ;
24
- $elem . on ( 'click' , ( ) => {
25
- $ ( this ) . val ( entry . display ) ;
26
- $ ( this ) . siblings ( 'input[type=hidden]' ) . val ( entry . dn ) ;
27
- $ ( this ) . siblings ( 'div.dn_link_suggestions' ) . empty ( ) ;
28
- } )
21
+ if ( data . entries ) {
22
+ // add entries to list
23
+ data . entries . forEach ( ( entry ) => {
24
+ const $elem = $ ( `<button type="button" class="list-group-item list-group-item-action">${ entry . display } </button>` ) ;
25
+ $elem . on ( 'click' , ( ) => {
26
+ $ ( this ) . val ( entry . display ) ;
27
+ $ ( this ) . siblings ( 'input[type=hidden]' ) . val ( entry . dn ) ;
28
+ $ ( this ) . siblings ( 'div.dn_link_suggestions' ) . empty ( ) ;
29
+ } )
30
+ $ ( this ) . siblings ( 'div.dn_link_suggestions' ) . append ( $elem ) ;
31
+ } ) ;
32
+ if ( data . warning ) {
33
+ const $elem = $ ( `<span class="list-group-item list-group-item-warning">${ data . warning } </span>` ) ;
34
+ $ ( this ) . siblings ( 'div.dn_link_suggestions' ) . append ( $elem ) ;
35
+ }
36
+ }
37
+ if ( data . error ) {
38
+ const $elem = $ ( `<span class="list-group-item list-group-item-danger">${ data . error } </span>` ) ;
29
39
$ ( this ) . siblings ( 'div.dn_link_suggestions' ) . append ( $elem ) ;
30
- } ) ;
40
+ }
31
41
} , 'json' ) ;
32
42
} , 500 ) ;
33
43
}
0 commit comments