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 |
/*execute a function when someone writes in the text field:*/ |
68 |
var token_counter = 0; |
69 |
inp.addEventListener("input", function(e) { |
70 |
var a, val = this.value; |
71 |
/* var for async compare */ |
72 |
var tmp_input = this.value.replace(/[+']/g, function(matched){ |
73 |
return query_url_decode[matched]; |
74 |
}); |
75 |
token_counter++; |
76 |
currentFocus = -1; |
77 |
if (document.getElementsByClassName("autocomplete-items").length !== 0){ |
78 |
a = document.getElementsByClassName("autocomplete-items")[0]; |
79 |
} else { |
80 |
/*create a DIV element that will contain the items (values):*/ |
81 |
a = document.createElement("DIV"); |
82 |
a.setAttribute("id", this.id + "autocomplete-list"); |
83 |
a.setAttribute("class", "autocomplete-items"); |
84 |
/*append the DIV element as a child of the autocomplete container:*/ |
85 |
this.parentNode.appendChild(a); |
86 |
/*append position absolute left/right:*/ |
87 |
$(".autocomplete-items").css("left",left); |
88 |
$(".autocomplete-items").css("right",right); |
89 |
}; |
90 |
/* get es_prefix key for builder */ |
91 |
var chose_prefix = (select_idx == null || select_idx.length == 0) ? '' : GetValueIdx(select_idx, nb); |
92 |
chose_prefix = chose_prefix.replace(/([^,])[,-]([^,].*)?$/, '$1'); |
93 |
if (chose_prefix !== null){ |
94 |
var prefix = es_prefix[chose_prefix].toString(); |
95 |
val = val.replace(/[+']/g, function(matched){ |
96 |
return query_url_encode[matched]; |
97 |
}); |
98 |
if (tmp_input == '' || tmp_input == null){ |
99 |
closeAllLists(); |
100 |
token_counter = 0; |
101 |
} else { |
102 |
$.ajax({ |
103 |
type: 'GET', |
104 |
url: url_request + val + '&key=' + key + '&prefix=' + prefix + '&token_counter=' + token_counter, |
105 |
success: function (data) { |
106 |
//console.log(data); |
107 |
if (data.length !== 0){ |
108 |
var myset; //Set for Autocomplete unique |
109 |
/* autocomplete for all prefix */ |
110 |
if (chose_prefix === 'kw' || chose_prefix === ''){ |
111 |
myset = GetSetAutocompleteAllIdx(data, prefix, key); |
112 |
} else { // autocomplete for one prefix |
113 |
myset = GetSetAutocompleteOneIdx(data, prefix, key); |
114 |
}; |
115 |
/* append set to autocomplete */ |
116 |
if ( tmp_input + prefix == data['val'] + data['prefix'] && token_counter === parseInt(data['token_counter'], 10)){ |
117 |
a.innerHTML = ""; |
118 |
for (let item of myset){ |
119 |
a.appendChild(CreateDivItemAutocomplete(item, val)); |
120 |
}; |
121 |
}; |
122 |
} else { |
123 |
closeAllLists(this); |
124 |
}; |
125 |
}, |
126 |
error: function (data) { |
127 |
console.log(data); |
128 |
}, |
129 |
}); |
130 |
} |
131 |
|
132 |
}; |
133 |
}); |
134 |
/* get value for tag with name idx */ |
135 |
function GetValueIdx(elem, nb){ |
136 |
switch (elem[0].tagName){ |
137 |
case 'INPUT': |
138 |
return elem[0].value; |
139 |
case 'SELECT': |
140 |
return select_idx[nb].options[select_idx[nb].selectedIndex].value; |
141 |
default: |
142 |
return null; |
143 |
}; |
144 |
}; |
145 |
/* get autocomplete for only one prefix title/author/etc... */ |
146 |
function GetSetAutocompleteOneIdx(data, prefix, key){ |
147 |
let myset = new Set(); |
148 |
let tmp_data = data['hits']['hits']; |
149 |
for (let i = 0; i < tmp_data.length; i++) { |
150 |
for (let j = 0; j < tmp_data[i]['highlight'][prefix + '.' + key].length; j++){ |
151 |
/* div with data for autocomplete */ |
152 |
let tmp = tmp_data[i]['highlight'][prefix + '.' + key][j]; |
153 |
tmp = tmp.replace(/^\[/g, ''); |
154 |
tmp = tmp.replace(/\]+$/g, ''); |
155 |
myset.add(tmp.replace(/^[ &\/\\#,+)$~%.'":*?>{}!;]+|[ &\/\\#,+($~%.'":*?<{}!;]+$/g, '')); |
156 |
if (myset.size >= nb_autocomplete) break; |
157 |
}; |
158 |
if (myset.size >= nb_autocomplete) break; |
159 |
}; |
160 |
return myset; |
161 |
}; |
162 |
/* get autocomplete for all prefix */ |
163 |
function GetSetAutocompleteAllIdx(data, prefix, key){ |
164 |
let myset = new Set(); |
165 |
var pref = prefix.split(","); |
166 |
for (k = 0; k < Object.keys(data).length; k++){ //Object.keys(data).length |
167 |
if (data[k] != '' && data[k] != null){ |
168 |
let tmp_data = data[k]['hits']['hits']; |
169 |
for (i = 0; i < tmp_data.length; i++) { |
170 |
for (j = 0; j < tmp_data[i]['highlight'][pref[k] + '.' + key].length; j++){ |
171 |
/* div with data for autocomplete */ |
172 |
let tmp = tmp_data[i]['highlight'][pref[k] + '.' + key][j] |
173 |
myset.add(tmp.replace(/[ &#,+()$~%.'":*?<{}!/;]+$/g, '')); |
174 |
if (myset.size >= nb_autocomplete) break; |
175 |
}; |
176 |
if (myset.size >= nb_autocomplete) break; |
177 |
}; |
178 |
if (myset.size >= nb_autocomplete) break; |
179 |
} |
180 |
} |
181 |
return myset; |
182 |
}; |
183 |
|
184 |
/*execute a function presses a key on the keyboard:*/ |
185 |
inp.addEventListener("keydown", function(e) { |
186 |
var x = document.getElementById(this.id + "autocomplete-list"); |
187 |
if (x) x = x.getElementsByTagName("div"); |
188 |
if (e.keyCode == 40) { //DOWN |
189 |
/*If the arrow DOWN key is pressed, |
190 |
increase the currentFocus variable:*/ |
191 |
currentFocus++; |
192 |
/*and and make the current item more visible:*/ |
193 |
addActive(x); |
194 |
} else if (e.keyCode == 38) { //up |
195 |
/*If the arrow UP key is pressed, |
196 |
decrease the currentFocus variable:*/ |
197 |
currentFocus--; |
198 |
/*and and make the current item more visible:*/ |
199 |
addActive(x); |
200 |
e.preventDefault(); |
201 |
} else if (e.keyCode == 13) { |
202 |
/*If the ENTER key is pressed, prevent the form from being submitted,*/ |
203 |
//e.preventDefault(); |
204 |
if (currentFocus > -1) { |
205 |
/*and simulate a click on the "active" item:*/ |
206 |
if (x) x[currentFocus].click(); |
207 |
} |
208 |
} |
209 |
/* press Esc clear all autocomplete */ |
210 |
else if (e.keyCode == 27) { |
211 |
closeAllLists(); |
212 |
} |
213 |
/* press Esc clear all autocomplete */ |
214 |
else if (e.keyCode == 8) { |
215 |
closeAllLists(); |
216 |
}; |
217 |
}); |
218 |
function addActive(x) { |
219 |
/*a function to classify an item as "active":*/ |
220 |
if (!x) return false; |
221 |
/*start by removing the "active" class on all items:*/ |
222 |
removeActive(x); |
223 |
if (currentFocus >= x.length) currentFocus = 0; |
224 |
if (currentFocus < 0) currentFocus = (x.length - 1); |
225 |
/*add class "autocomplete-active":*/ |
226 |
x[currentFocus].classList.add("autocomplete-active"); |
227 |
inp.value = (x[currentFocus].textContent.replace(/<\/?[^>]+(>|$)/g, "")).trim(); |
228 |
}; |
229 |
function removeActive(x) { |
230 |
/*a function to remove the "active" class from all autocomplete items:*/ |
231 |
for (var i = 0; i < x.length; i++) { |
232 |
x[i].classList.remove("autocomplete-active"); |
233 |
}; |
234 |
}; |
235 |
|
236 |
function closeAllLists(elem) { |
237 |
/*close all autocomplete lists in the document with class autocomplete-items */ |
238 |
var x = document.getElementsByClassName("autocomplete-items"); |
239 |
for (var i = 0; i < x.length; i++) { |
240 |
x[i].parentNode.removeChild(x[i]) |
241 |
}; |
242 |
}; |
243 |
|
244 |
/* div for one item autocomplete */ |
245 |
function CreateDivItemAutocomplete (elem){ |
246 |
var b = document.createElement("DIV"); |
247 |
// add element "; |
248 |
b.innerHTML += elem; |
249 |
/*insert a input field that will hold the current array item's value:*/ |
250 |
b.innerHTML += "<input type='hidden' value=''>"; |
251 |
/*execute a function when someone clicks on the item value (DIV element):*/ |
252 |
b.addEventListener("click", function(e) { |
253 |
/* insert the value for the autocomplete text field: */ |
254 |
inp.value = this.getElementsByTagName("input")[0].value; |
255 |
/* normalyzer hightlight without tags */ |
256 |
//inp.value = (inp.value.replace(/<\/?[^>]+(>|$)/g, "")).trim(); |
257 |
inp.value = this.innerText; |
258 |
/* Submit form click mouse in div */ |
259 |
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(); |