1 function patron_autocomplete(node, options) {
4 let on_select_callback;
5 let leading_wildcard = patronAutoCompleteSearchMethod === 'contains' ? '%' : '';
7 if ( options['link-to'] ) {
8 link_to = options['link-to'];
10 if ( options['url-params'] ) {
11 url_params = options['url-params'];
13 if ( options['on-select-callback'] ) {
14 on_select_callback = options['on-select-callback'];
17 return node.autocomplete({
18 source: function( request, response ) {
19 let subquery_and = [];
20 request.term.split(/[\s,]+/)
21 .filter(function(s){ return s.length })
22 .forEach(function(pattern,i){
24 defaultPatronSearchFields.split(',').forEach(function(field,i){
26 {["me."+field]: {'like': leading_wildcard + pattern + '%'}}
29 subquery_and.push(subquery_or);
31 let q = {"-and": subquery_and};
35 'q': JSON.stringify(q),
36 '_order_by': '+me.surname,+me.firstname',
41 url: '/api/v1/patrons',
43 "x-koha-embed": "library"
45 success: function(data) {
46 return response(data);
49 if ( e.state() != 'rejected' ) {
50 alert( __("An error occurred. Check the logs") );
57 select: function( event, ui ) {
59 window.location.href = ui.item.link;
60 } else if ( on_select_callback ) {
61 return on_select_callback(event, ui);
64 focus: function( event, ui ) {
65 event.preventDefault(); // Don't replace the text field
68 .data( "ui-autocomplete" )
69 ._renderItem = function( ul, item ) {
71 item.link = link_to == 'circ'
72 ? "/cgi-bin/koha/circ/circulation.pl"
73 : link_to == 'reserve'
74 ? "/cgi-bin/koha/reserve/request.pl"
75 : "/cgi-bin/koha/members/moremember.pl";
76 item.link += ( url_params ? '?' + url_params + '&' : "?" ) + 'borrowernumber=' + item.patron_id;
82 if( item.cardnumber != "" ){
83 // Display card number in parentheses if it exists
84 cardnumber = " (" + item.cardnumber + ") ";
86 if( item.library_id == loggedInLibrary ){
87 loggedInClass = "ac-currentlibrary";
91 return $( "<li></li>" )
92 .addClass( loggedInClass )
93 .data( "ui-autocomplete-item", item )
96 + ( item.link ? "<a href=\"" + item.link + "\">" : "<a>" )
97 + ( item.surname ? item.surname.escapeHtml() : "" ) + ", "
98 + ( item.firstname ? item.firstname.escapeHtml() : "" )
99 + cardnumber.escapeHtml()
101 + ( item.date_of_birth
102 ? $date(item.date_of_birth)
103 + "<span class=\"age_years\"> ("
104 + $get_age(item.date_of_birth)
110 + $format_address(item, { no_line_break: true, include_li: false }) + " "
111 + ( !singleBranchMode
113 "<span class=\"ac-library\">"
114 + item.library.name.escapeHtml()