PageSourceSearch

https://www.ville-thiais.fr/wp-content/plugins/autocomplete-for-re…/awesomplete.js?ver=0.1

js ville-thiais.fr collected 2026-10-02 11:24:50 UTC 9,490 bytes, 447 lines download raw bytes

1/**
2 * Simple, lightweight, usable local autocomplete library for modern browsers
3 * Because there weren’t enough autocomplete scripts in the world? Because I’m completely insane and have NIH syndrome? Probably both. :P
4 * @author Lea Verou http://leaverou.github.io/awesomplete
5 * MIT license
6 */
7
8(function () {
9
10var _ = function (input, o) {
11	var me = this;
12
13	// Setup
14
15	this.isOpened = false;
16
17	this.input = $(input);
18	this.input.setAttribute("autocomplete", "off");
19	this.input.setAttribute("aria-autocomplete", "list");
20
21	o = o || {};
22
23	configure(this, {
24		minChars: 2,
25		maxItems: 10,
26		autoFirst: false,
27		data: _.DATA,
28		filter: _.FILTER_CONTAINS,
29		sort: _.SORT_BYLENGTH,
30		item: _.ITEM,
31		replace: _.REPLACE
32	}, o);
33
34	this.index = -1;
35
36	// Create necessary elements
37
38	this.container = $.create("div", {
39		className: "awesomplete",
40		around: input
41	});
42
43	this.ul = $.create("ul", {
44		hidden: "hidden",
45		inside: this.container
46	});
47
48	this.status = $.create("span", {
49		className: "visually-hidden",
50		role: "status",
51		"aria-live": "assertive",
52		"aria-relevant": "additions",
53		inside: this.container
54	});
55
56	// Bind events
57
58	$.bind(this.input, {
59		"input": this.evaluate.bind(this),
60		"blur": this.close.bind(this, { reason: "blur" }),
61		"keydown": function(evt) {
62			var c = evt.keyCode;
63
64			// If the dropdown `ul` is in view, then act on keydown for the following keys:
65			// Enter / Esc / Up / Down
66			if(me.opened) {
67				if (c === 13 && me.selected) { // Enter
68					evt.preventDefault();
69					me.select();
70				}
71				else if (c === 27) { // Esc
72					me.close({ reason: "esc" });
73				}
74				else if (c === 38 || c === 40) { // Down/Up arrow
75					evt.preventDefault();
76					me[c === 38? "previous" : "next"]();
77				}
78			}
79		}
80	});
81
82	$.bind(this.input.form, {"submit": this.close.bind(this, { reason: "submit" })});
83
84	$.bind(this.ul, {"mousedown": function(evt) {
85		var li = evt.target;
86
87		if (li !== this) {
88
89			while (li && !/li/i.test(li.nodeName)) {
90				li = li.parentNode;
91			}
92
93			if (li && evt.button === 0) {  // Only select on left click
94				evt.preventDefault();
95				me.select(li, evt.target);
96			}
97		}
98	}});
99
100	if (this.input.hasAttribute("list")) {
101		this.list = "#" + this.input.getAttribute("list");
102		this.input.removeAttribute("list");
103	}
104	else {
105		this.list = this.input.getAttribute("data-list") || o.list || [];
106	}
107
108	_.all.push(this);
109};
110
111_.prototype = {
112	set list(list) {
113		if (Array.isArray(list)) {
114			this._list = list;
115		}
116		else if (typeof list === "string" && list.indexOf(",") > -1) {
117				this._list = list.split(/\s*,\s*/);
118		}
119		else { // Element or CSS selector
120			list = $(list);
121
122			if (list && list.children) {
123				var items = [];
124				slice.apply(list.children).forEach(function (el) {
125					if (!el.disabled) {
126						var text = el.textContent.trim();
127						var value = el.value || text;
128						var label = el.label || text;
129						if (value !== "") {
130							items.push({ label: label, value: value });
131						}
132					}
133				});
134				this._list = items;
135			}
136		}
137
138		if (document.activeElement === this.input) {
139			this.evaluate();
140		}
141	},
142
143	get selected() {
144		return this.index > -1;
145	},
146
147	get opened() {
148		return this.isOpened;
149	},
150
151	close: function (o) {
152		if (!this.opened) {
153			return;
154		}
155
156		this.ul.setAttribute("hidden", "");
157		this.isOpened = false;
158		this.index = -1;
159
160		$.fire(this.input, "awesomplete-close", o || {});
161	},
162
163	open: function () {
164		this.ul.removeAttribute("hidden");
165		this.isOpened = true;
166
167		if (this.autoFirst && this.index === -1) {
168			this.goto(0);
169		}
170
171		$.fire(this.input, "awesomplete-open");
172	},
173
174	next: function () {
175		var count = this.ul.children.length;
176		this.goto(this.index < count - 1 ? this.index + 1 : (count ? 0 : -1) );
177	},
178
179	previous: function () {
180		var count = this.ul.children.length;
181		var pos = this.index - 1;
182
183		this.goto(this.selected && pos !== -1 ? pos : count - 1);
184	},
185
186	// Should not be used, highlights specific item without any checks!
187	goto: function (i) {
188		var lis = this.ul.children;
189
190		if (this.selected) {
191			lis[this.index].setAttribute("aria-selected", "false");
192		}
193
194		this.index = i;
195
196		if (i > -1 && lis.length > 0) {
197			lis[i].setAttribute("aria-selected", "true");
198			this.status.textContent = lis[i].textContent;
199
200			$.fire(this.input, "awesomplete-highlight", {
201				text: this.suggestions[this.index]
202			});
203		}
204	},
205
206	select: function (selected, origin) {
207		if (selected) {
208			this.index = $.siblingIndex(selected);
209		} else {
210			selected = this.ul.children[this.index];
211		}
212
213		if (selected) {
214			var suggestion = this.suggestions[this.index];
215
216			var allowed = $.fire(this.input, "awesomplete-select", {
217				text: suggestion,
218				origin: origin || selected
219			});
220
vendor: 4,180 bytes, lines 221-413
221			if (allowed) {
222				this.replace(suggestion);
223				this.close({ reason: "select" });
224				$.fire(this.input, "awesomplete-selectcomplete", {
225					text: suggestion
226				});
227			}
228		}
229	},
230
231	evaluate: function() {
232		var me = this;
233		var value = this.input.value;
234
235		if (value.length >= this.minChars && this._list.length > 0) {
236			this.index = -1;
237			// Populate list with options that match
238			this.ul.innerHTML = "";
239
240			this.suggestions = this._list
241				.map(function(item) {
242					return new Suggestion(me.data(item, value));
243				})
244				.filter(function(item) {
245					return me.filter(item, value);
246				})
247				.sort(this.sort)
248				.slice(0, this.maxItems);
249
250			this.suggestions.forEach(function(text) {
251					me.ul.appendChild(me.item(text, value));
252				});
253
254			if (this.ul.children.length === 0) {
255				this.close({ reason: "nomatches" });
256			} else {
257				this.open();
258			}
259		}
260		else {
261			this.close({ reason: "nomatches" });
262		}
263	}
264};
265
266// Static methods/properties
267
268_.all = [];
269
270_.FILTER_CONTAINS = function (text, input) {
271	return RegExp($.regExpEscape(input.trim()), "i").test(text);
272};
273
274_.FILTER_STARTSWITH = function (text, input) {
275	return RegExp("^" + $.regExpEscape(input.trim()), "i").test(text);
276};
277
278_.SORT_BYLENGTH = function (a, b) {
279	if (a.length !== b.length) {
280		return a.length - b.length;
281	}
282
283	return a < b? -1 : 1;
284};
285
286_.ITEM = function (text, input) {
287	var html = input.trim() === '' ? text : text.replace(RegExp($.regExpEscape(input.trim()), "gi"), "<mark>$&</mark>");
288	return $.create("li", {
289		innerHTML: html,
290		"aria-selected": "false"
291	});
292};
293
294_.REPLACE = function (text) {
295	this.input.value = text.value;
296};
297
298_.DATA = function (item/*, input*/) { return item; };
299
300// Private functions
301
302function Suggestion(data) {
303	var o = Array.isArray(data)
304	  ? { label: data[0], value: data[1] }
305	  : typeof data === "object" && "label" in data && "value" in data ? data : { label: data, value: data };
306
307	this.label = o.label || o.value;
308	this.value = o.value;
309}
310Object.defineProperty(Suggestion.prototype = Object.create(String.prototype), "length", {
311	get: function() { return this.label.length; }
312});
313Suggestion.prototype.toString = Suggestion.prototype.valueOf = function () {
314	return "" + this.label;
315};
316
317function configure(instance, properties, o) {
318	for (var i in properties) {
319		var initial = properties[i],
320		    attrValue = instance.input.getAttribute("data-" + i.toLowerCase());
321
322		if (typeof initial === "number") {
323			instance[i] = parseInt(attrValue);
324		}
325		else if (initial === false) { // Boolean options must be false by default anyway
326			instance[i] = attrValue !== null;
327		}
328		else if (initial instanceof Function) {
329			instance[i] = null;
330		}
331		else {
332			instance[i] = attrValue;
333		}
334
335		if (!instance[i] && instance[i] !== 0) {
336			instance[i] = (i in o)? o[i] : initial;
337		}
338	}
339}
340
341// Helpers
342
343var slice = Array.prototype.slice;
344
345function $(expr, con) {
346	return typeof expr === "string"? (con || document).querySelector(expr) : expr || null;
347}
348
349function $$(expr, con) {
350	return slice.call((con || document).querySelectorAll(expr));
351}
352
353$.create = function(tag, o) {
354	var element = document.createElement(tag);
355
356	for (var i in o) {
357		var val = o[i];
358
359		if (i === "inside") {
360			$(val).appendChild(element);
361		}
362		else if (i === "around") {
363			var ref = $(val);
364			ref.parentNode.insertBefore(element, ref);
365			element.appendChild(ref);
366		}
367		else if (i in element) {
368			element[i] = val;
369		}
370		else {
371			element.setAttribute(i, val);
372		}
373	}
374
375	return element;
376};
377
378$.bind = function(element, o) {
379	if (element) {
380		for (var event in o) {
381			var callback = o[event];
382
383			event.split(/\s+/).forEach(function (event) {
384				element.addEventListener(event, callback);
385			});
386		}
387	}
388};
389
390$.fire = function(target, type, properties) {
391	var evt = document.createEvent("HTMLEvents");
392
393	evt.initEvent(type, true, true );
394
395	for (var j in properties) {
396		evt[j] = properties[j];
397	}
398
399	return target.dispatchEvent(evt);
400};
401
402$.regExpEscape = function (s) {
403	return s.replace(/[-\\^$*+?.()|[\]{}]/g, "\\$&");
404};
405
406$.siblingIndex = function (el) {
407	/* eslint-disable no-cond-assign */
408	for (var i = 0; el = el.previousElementSibling; i++);
409	return i;
410};
411
412// Initialization
413
414function init() {
415	$$("input.awesomplete").forEach(function (input) {
416		new _(input);
417	});
418}
419
420// Are we in a browser? Check for Document constructor
421if (typeof Document !== "undefined") {
422	// DOM already loaded?
423	if (document.readyState !== "loading") {
424		init();
425	}
426	else {
427		// Wait for it
428		document.addEventListener("DOMContentLoaded", init);
429	}
430}
431
432_.$ = $;
433_.$$ = $$;
434
435// Make sure to export Awesomplete on self when in a browser
436if (typeof self !== "undefined") {
437	self.Awesomplete = _;
438}
439
440// Expose Awesomplete as a CJS module
441if (typeof module === "object" && module.exports) {
442	module.exports = _;
443}
444
445return _;
446
447}());

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.