PageSourceSearch

https://www.technologieland-hessen.de/solr/solr_suggest.js

js technologieland-hessen.de collected 2026-10-02 12:56:22 UTC 4,439 bytes, 223 lines download raw bytes

1const SolrSuggest = (function ($) {
2	"use strict";
3
4	const config = {
5		endpoint: "SolrSuggest.cfc?method=getSuggestions&returnformat=json",
6		searchPageUrl: "suche",
7		searchParamName: "q",
8		collection: "tlh",
9		minChars: 2,
10		delay: 180,
11	};
12
13	function debounce(fn, delay) {
14		let timer = null;
15
16		return function () {
17			const args = arguments;
18			clearTimeout(timer);
19
20			timer = setTimeout(function () {
21				fn.apply(null, args);
22			}, delay);
23		};
24	}
25
26	function stripHtml(html) {
27		return $("<div>").html(html).text();
28	}
29
30	function initInstance($wrapper) {
31		const $input = $wrapper.find(".solr-suggest-input");
32		const $box = $wrapper.find(".solr-suggest-box");
33
34		let request = null;
35		let activeIndex = -1;
36		let currentItems = [];
37
38		if (!$input.length || !$box.length) {
39			return;
40		}
41
42		function renderSuggestions(items) {
43			currentItems = items || [];
44			activeIndex = -1;
45			$box.empty();
46
47			if (!currentItems.length) {
48				$box.hide();
49				return;
50			}
51
52			$.each(currentItems, function (index, item) {
53				const count = typeof item.COUNT !== "undefined" ? " <small>(" + item.COUNT + " Treffer)</small>" : "";
54
55				$("<div>", {
56					class: "suggestion",
57				})
58					.html((item.LABEL || item.VALUE || "") + count)
59					.on("mousedown", function (event) {
60						event.preventDefault();
61						chooseSuggestion(index);
62					})
63					.appendTo($box);
64			});
65
66			$box.show();
67		}
68
69		function chooseSuggestion(index) {
70			const item = currentItems[index];
71
72			if (!item) {
73				return;
74			}
75
76			const value = stripHtml(item.VALUE || item.LABEL || "");
77			const form = $input.closest("form")[0];
78
79			$input.val(value);
80
81			if (form) {
82				if (typeof form.requestSubmit === "function") {
83					form.requestSubmit();
84				} else {
85					form.submit();
86				}
87
88				return;
89			}
90
91			const url = new URL(config.searchPageUrl, window.location.origin);
92
93			url.searchParams.set(config.searchParamName, value);
94
95			const collection = $("#collection").val();
96
97			if (collection) {
98				url.searchParams.set("collection", collection);
99			}
100
101			window.location.href = url.toString();
102		}
103
104		function loadSuggestions() {
105			const term = $.trim($input.val());
106
107			if (term.length < config.minChars) {
108				if (request) {
109					request.abort();
110				}
111
112				$box.hide();
113				return;
114			}
115
116			if (request) {
117				request.abort();
118			}
119
120			request = $.ajax({
121				url: config.endpoint,
122				method: "GET",
123				dataType: "json",
124				data: {
125					collection: config.collection,
126					term: term,
127					count: 10,
128				},
129			});
130
131			const currentRequest = request;
132
133			currentRequest
134				.done(function (data) {
135					renderSuggestions(data);
136				})
137				.fail(function (jqXHR, textStatus) {
138					if (textStatus === "abort") {
139						return;
140					}
141
142					$box.hide();
143				})
144				.always(function () {
145					if (request === currentRequest) {
146						request = null;
147					}
148				});
149		}
150
151		const debouncedLoad = debounce(loadSuggestions, config.delay);
152
153		$input.on("input", debouncedLoad);
154
155		$input.on("keydown", function (event) {
156			if (!currentItems.length) {
157				return;
158			}
159
160			if (event.key === "ArrowDown") {
161				event.preventDefault();
162
163				activeIndex = Math.min(activeIndex + 1, currentItems.length - 1);
164			} else if (event.key === "ArrowUp") {
165				event.preventDefault();
166
167				activeIndex = Math.max(activeIndex - 1, 0);
168			} else if (event.key === "Enter" && activeIndex >= 0) {
169				event.preventDefault();
170				chooseSuggestion(activeIndex);
171			} else if (event.key === "Escape") {
172				$box.hide();
173				return;
174			} else {
175				return;
176			}
177
178			$box.children().removeClass("is-active").eq(activeIndex).addClass("is-active");
179		});
180
181		$input.on("blur", function () {
182			setTimeout(function () {
183				$box.hide();
184			}, 150);
185		});
186	}
187
188	function init(options) {
189		$.extend(config, options || {});
190
191		$(".solr-suggest-wrapper").each(function () {
192			initInstance($(this));
193		});
194	}
195
196	return {
197		init: init,
198	};
199})(jQuery);
200
201jQuery(function () {
202	SolrSuggest.init({
203		endpoint: "/solr/SolrSuggest.cfc?method=getSuggestions&returnformat=json",
204		searchPageUrl: "/suche",
205		searchParamName: "q",
206	});
207});
208function showSuche() {
209	var searchForm = jQuery("[id='SucheForm']");
210
211	if (searchForm.css("display") === "block") {
212		return closeSuche();
213	}
214
215	searchForm.show().attr("aria-hidden", "false");
216	jQuery("[id='search']").trigger("focus");
217	return false;
218}
219
220function closeSuche() {
221	jQuery("[id='SucheForm']").hide().attr("aria-hidden", "true");
222	return false;
223}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.