|
Line 0
Link Here
|
|
|
1 |
/* OPAC JS file OPACAutocompleteElasticSearch */ |
| 2 |
/* prefix for search ES */ |
| 3 |
var es_prefix = { |
| 4 |
'au': 'author', |
| 5 |
'pb': 'publisher', |
| 6 |
'se': 'title-series', |
| 7 |
'su': 'subject', |
| 8 |
'ti': 'title-cover', |
| 9 |
/* for all */ |
| 10 |
'': ['title-cover', 'author', 'subject', 'title-series', 'publisher'], |
| 11 |
'kw': ['title-cover', 'author', 'subject', 'title-series', 'publisher'] |
| 12 |
}; |
| 13 |
|
| 14 |
/* stop class for elements name=["q"] */ |
| 15 |
var stop_class_input = ["form-field-value"]; |
| 16 |
|
| 17 |
var url_request = '/cgi-bin/koha/api/elasticsearch/elasticsearch.pl?q='; |
| 18 |
/* query for elasticsearch encode*/ |
| 19 |
var query_url_encode = { |
| 20 |
'\'':'%5C%27', /* \\' for decode */ |
| 21 |
'+': '' |
| 22 |
}; |
| 23 |
/* query for elasticsearch decode*/ |
| 24 |
var query_url_decode = { |
| 25 |
'\'':"\\'", |
| 26 |
'+': '' |
| 27 |
}; |
| 28 |
/* count of lines for autocomplete */ |
| 29 |
var nb_autocomplete = 10; |
| 30 |
/* key API */ |
| 31 |
var key = 'autocomplete'; |
| 32 |
|
| 33 |
function AutocompleteInitIntranet(){ |
| 34 |
/* vars for class position absolute autocomplete */ |
| 35 |
var left = "0px"; |
| 36 |
var right = "0px"; |
| 37 |
var top = ""; |
| 38 |
/* get all input name q for search */ |
| 39 |
var input_q = document.getElementsByName("q"); |
| 40 |
for (var nb = 0; nb < input_q.length; nb++){ |
| 41 |
/* addEventListener for every 'input' */ |
| 42 |
if (!stop_class_input.includes(input_q[nb].className)){ |
| 43 |
autocomplete(input_q[nb], nb, left, right, top); |
| 44 |
} |
| 45 |
}; |
| 46 |
}; |
| 47 |
|
| 48 |
function autocomplete(inp, nb, left, right) { |
| 49 |
var select_idx = document.getElementsByName("idx"); |
| 50 |
/* autocomplete off for input */ |
| 51 |
inp.setAttribute("autocomplete", "off"); |
| 52 |
/* get parent of input */ |
| 53 |
var parent_inp = $(inp).parent(); |
| 54 |
/* get element after input */ |
| 55 |
var next_elem_inp = inp.nextElementSibling; |
| 56 |
/* create new div with position relative for class .autocomplete with absolute */ |
| 57 |
var div_relative = document.createElement('div'); |
| 58 |
$(div_relative).addClass( "autocomplete" ); |
| 59 |
div_relative.append(inp); |
| 60 |
/* input doesn't have an elem after, add it to parent */ |
| 61 |
if (next_elem_inp === null){ |
| 62 |
parent_inp.append( div_relative ); |
| 63 |
} else { // input has an elem after, add elem after it |
| 64 |
next_elem_inp.before(div_relative); |
| 65 |
}; |
| 66 |
var currentFocus; |
| 67 |
|
| 68 |
var timer; |
| 69 |
var doneTimerInterval = 500; |
| 70 |
|
| 71 |
/*execute a function when someone writes in the text field:*/ |
| 72 |
var token_counter = 0; |
| 73 |
inp.addEventListener("input", function(e) { |
| 74 |
clearTimeout(timer); |
| 75 |
timer = setTimeout( function() { |
| 76 |
var a, val = inp.value; |
| 77 |
/* var for async compare */ |
| 78 |
var tmp_input = inp.value.replace(/[+']/g, function(matched){ |
| 79 |
return query_url_decode[matched]; |
| 80 |
}); |
| 81 |
token_counter++; |
| 82 |
currentFocus = -1; |
| 83 |
if (document.getElementsByClassName("autocomplete-items").length !== 0){ |
| 84 |
a = document.getElementsByClassName("autocomplete-items")[0]; |
| 85 |
} else { |
| 86 |
/*create a DIV element that will contain the items (values):*/ |
| 87 |
a = document.createElement("DIV"); |
| 88 |
a.setAttribute("id", inp.id + "autocomplete-list"); |
| 89 |
a.setAttribute("class", "autocomplete-items"); |
| 90 |
/*append the DIV element as a child of the autocomplete container:*/ |
| 91 |
inp.parentNode.appendChild(a); |
| 92 |
/*append position absolute left/right:*/ |
| 93 |
$(".autocomplete-items").css("left",left); |
| 94 |
$(".autocomplete-items").css("right",right); |
| 95 |
}; |
| 96 |
/* get es_prefix key for builder */ |
| 97 |
var chose_prefix = (select_idx == null || select_idx.length == 0) ? '' : GetValueIdx(select_idx, nb); |
| 98 |
chose_prefix = chose_prefix.replace(/([^,])[,-]([^,].*)?$/, '$1'); |
| 99 |
if (chose_prefix !== null){ |
| 100 |
var prefix = es_prefix[chose_prefix].toString(); |
| 101 |
val = val.replace(/[+']/g, function(matched){ |
| 102 |
return query_url_encode[matched]; |
| 103 |
}); |
| 104 |
if (tmp_input == '' || tmp_input == null){ |
| 105 |
closeAllLists(); |
| 106 |
token_counter = 0; |
| 107 |
} else { |
| 108 |
$.ajax({ |
| 109 |
type: 'GET', |
| 110 |
url: url_request + val + '&key=' + key + '&prefix=' + prefix + '&token_counter=' + token_counter, |
| 111 |
contentType: "application/json;charset=UTF-8", |
| 112 |
success: function (data) { |
| 113 |
//console.log(data); |
| 114 |
if (data.length !== 0){ |
| 115 |
var myset; //Set for Autocomplete unique |
| 116 |
|
| 117 |
myset = GetSetAutocomplete(data, prefix, key); |
| 118 |
/* append set to autocomplete */ |
| 119 |
if ( tmp_input + prefix == data['val'] + data['prefix'] && token_counter === parseInt(data['token_counter'], 10)){ |
| 120 |
a.innerHTML = ""; |
| 121 |
for (let item of myset){ |
| 122 |
a.appendChild(CreateDivItemAutocomplete(item, val)); |
| 123 |
}; |
| 124 |
}; |
| 125 |
} else { |
| 126 |
closeAllLists(inp); |
| 127 |
}; |
| 128 |
}, |
| 129 |
error: function (data) { |
| 130 |
console.log(data); |
| 131 |
}, |
| 132 |
}); |
| 133 |
} |
| 134 |
|
| 135 |
}; |
| 136 |
}, doneTimerInterval); |
| 137 |
}); |
| 138 |
/* get value for tag with name idx */ |
| 139 |
function GetValueIdx(elem, nb){ |
| 140 |
switch (elem[0].tagName){ |
| 141 |
case 'INPUT': |
| 142 |
return elem[0].value; |
| 143 |
case 'SELECT': |
| 144 |
return select_idx[nb].options[select_idx[nb].selectedIndex].value; |
| 145 |
default: |
| 146 |
return null; |
| 147 |
}; |
| 148 |
}; |
| 149 |
/* get autocomplete for all prefix */ |
| 150 |
function GetSetAutocomplete(data, prefix, key){ |
| 151 |
let myset = new Set(); |
| 152 |
var pref = prefix.split(","); |
| 153 |
for (k = 0; k < Object.keys(data).length; k++){ //Object.keys(data).length |
| 154 |
if (data[k] != '' && data[k] != null){ |
| 155 |
let tmp_data = data[k]['hits']['hits']; |
| 156 |
for (i = 0; i < tmp_data.length; i++) { |
| 157 |
for (j = 0; j < tmp_data[i]['highlight'][pref[k] + '.' + key].length; j++){ |
| 158 |
/* div with data for autocomplete */ |
| 159 |
let tmp = tmp_data[i]['highlight'][pref[k] + '.' + key][j] |
| 160 |
myset.add(tmp.replace(/[ &#,+()$~%.'":*?<{}!/;]+$/g, '')); |
| 161 |
if (myset.size >= nb_autocomplete) break; |
| 162 |
}; |
| 163 |
if (myset.size >= nb_autocomplete) break; |
| 164 |
}; |
| 165 |
if (myset.size >= nb_autocomplete) break; |
| 166 |
} |
| 167 |
} |
| 168 |
return myset; |
| 169 |
}; |
| 170 |
|
| 171 |
/*execute a function presses a key on the keyboard:*/ |
| 172 |
inp.addEventListener("keydown", function(e) { |
| 173 |
var x = document.getElementById(this.id + "autocomplete-list"); |
| 174 |
if (x) x = x.getElementsByTagName("div"); |
| 175 |
if (e.keyCode == 40) { //DOWN |
| 176 |
/*If the arrow DOWN key is pressed, |
| 177 |
increase the currentFocus variable:*/ |
| 178 |
currentFocus++; |
| 179 |
/*and and make the current item more visible:*/ |
| 180 |
addActive(x); |
| 181 |
} else if (e.keyCode == 38) { //up |
| 182 |
/*If the arrow UP key is pressed, |
| 183 |
decrease the currentFocus variable:*/ |
| 184 |
currentFocus--; |
| 185 |
/*and and make the current item more visible:*/ |
| 186 |
addActive(x); |
| 187 |
e.preventDefault(); |
| 188 |
} else if (e.keyCode == 13) { |
| 189 |
/*If the ENTER key is pressed, prevent the form from being submitted,*/ |
| 190 |
//e.preventDefault(); |
| 191 |
if (currentFocus > -1) { |
| 192 |
/*and simulate a click on the "active" item:*/ |
| 193 |
if (x) x[currentFocus].click(); |
| 194 |
} |
| 195 |
} |
| 196 |
/* press Esc clear all autocomplete */ |
| 197 |
else if (e.keyCode == 27) { |
| 198 |
closeAllLists(); |
| 199 |
} |
| 200 |
/* press Esc clear all autocomplete */ |
| 201 |
else if (e.keyCode == 8) { |
| 202 |
closeAllLists(); |
| 203 |
} |
| 204 |
/* press Tab clear all autocomplete */ |
| 205 |
else if (e.keyCode == 9) { |
| 206 |
closeAllLists(); |
| 207 |
}; |
| 208 |
}); |
| 209 |
function addActive(x) { |
| 210 |
/*a function to classify an item as "active":*/ |
| 211 |
if (!x) return false; |
| 212 |
/*start by removing the "active" class on all items:*/ |
| 213 |
removeActive(x); |
| 214 |
if (currentFocus >= x.length) currentFocus = 0; |
| 215 |
if (currentFocus < 0) currentFocus = (x.length - 1); |
| 216 |
/*add class "autocomplete-active":*/ |
| 217 |
x[currentFocus].classList.add("autocomplete-active"); |
| 218 |
inp.value = (x[currentFocus].textContent.replace(/<\/?[^>]+(>|$)/g, "")).trim(); |
| 219 |
}; |
| 220 |
function removeActive(x) { |
| 221 |
/*a function to remove the "active" class from all autocomplete items:*/ |
| 222 |
for (var i = 0; i < x.length; i++) { |
| 223 |
x[i].classList.remove("autocomplete-active"); |
| 224 |
}; |
| 225 |
}; |
| 226 |
|
| 227 |
function closeAllLists(elem) { |
| 228 |
/*close all autocomplete lists in the document with class autocomplete-items */ |
| 229 |
var x = document.getElementsByClassName("autocomplete-items"); |
| 230 |
for (var i = 0; i < x.length; i++) { |
| 231 |
x[i].parentNode.removeChild(x[i]) |
| 232 |
}; |
| 233 |
}; |
| 234 |
|
| 235 |
/* div for one item autocomplete */ |
| 236 |
function CreateDivItemAutocomplete (elem){ |
| 237 |
var b = document.createElement("DIV"); |
| 238 |
// add element "; |
| 239 |
b.innerHTML += elem; |
| 240 |
/*insert a input field that will hold the current array item's value:*/ |
| 241 |
b.innerHTML += "<input type='hidden' value=''>"; |
| 242 |
/*execute a function when someone clicks on the item value (DIV element):*/ |
| 243 |
b.addEventListener("click", function(e) { |
| 244 |
/* insert the value for the autocomplete text field: */ |
| 245 |
inp.value = this.getElementsByTagName("input")[0].value; |
| 246 |
/* normalyzer hightlight without tags */ |
| 247 |
//inp.value = (inp.value.replace(/<\/?[^>]+(>|$)/g, "")).trim(); |
| 248 |
inp.value = this.innerText; |
| 249 |
|
| 250 |
var autocommit = 1; |
| 251 |
const inputs = document.querySelectorAll("#advanced-search input[type='text']"); |
| 252 |
for (var i = 0; i < inputs.length && autocommit; i++) { |
| 253 |
var input = inputs[i]; |
| 254 |
if (input === inp) { |
| 255 |
autocommit = 0; |
| 256 |
} |
| 257 |
} |
| 258 |
//Submit form click mouse in div if not in advanced search |
| 259 |
if (autocommit) this.closest("form").submit(); |
| 260 |
}); |
| 261 |
return b; |
| 262 |
}; |
| 263 |
|
| 264 |
/*execute a function when someone clicks in the document:*/ |
| 265 |
document.addEventListener("click", function (e) { |
| 266 |
closeAllLists(e.target); |
| 267 |
}); |
| 268 |
}; |
| 269 |
|
| 270 |
AutocompleteInitIntranet(); |