PageSourceSearch

https://www.simplifia.fr/wp-content/plugins/simpli-wp-obituary/Wid…/js/SearchBarAutocomplete.js?ver=1790079031

js simplifia.fr collected 2026-10-02 07:09:17 UTC 7,008 bytes, 170 lines download raw bytes

1$(document).ready(function () {
2    var inputName = $("#nameInput");
3    if (!inputName.length || typeof smpSearchBar === "undefined") {
4        return;
5    }
6    var timeout;
7    var urlObituary = window.smpDecode(smpSearchBar.urlObituary);
8    var searchByNameUrl = window.smpDecode(smpSearchBar.searchByNameUrl);
9
10    inputName.keydown(function () {
11        clearTimeout(timeout);
12        if (inputName.val().length >= 3) {
13            timeout = setTimeout(function () {
14                autocomplete(inputName, searchByNameUrl, function (data) {
15                    var string = "<div class=\"autocompletetable\" ><table class=\"clickable autocpl highlight\" style=\"color: black;background-color: white; \"><tbody>";
16                    data.obituaries.forEach(function (obituary) {
17                        var obituaryDate = new Date(obituary.creationDate * 1000);
18
19                        function formatDate(date) {
20                            var d = new Date(date),
21                                month = '' + (d.getMonth() + 1),
22                                day = '' + d.getDate(),
23                                year = d.getFullYear();
24
25                            if (month.length < 2) month = '0' + month;
26                            if (day.length < 2) day = '0' + day;
27
28                            return [day, month, year].join('/');
29                        }
30
31                        var age = (obituary.defunct?.age && obituary.defunct?.age !== "") ? "- " + obituary.defunct?.age + translation.ans : "";
32                        string += "<tr data-href=\"/" + urlObituary + "/" + obituary.defunct?.slug + "\"><td><div class=\"light font-13\" style=\"margin-bottom:5px;color:grey;\">" + translation.publieLe + " " + formatDate(obituaryDate) + "</div><div><span style=\"font-weight:500;\">" + obituary.defunct?.surname + " " + obituary.defunct?.name + "</span>" + " " + "<span class=\"age\">" + age + "</span></div></td><td width=\"40%\" class=\"valign-wrapper \"><span class=\"smp-icon material-icons-round left grey-text\" data-icon-family=\"Material+Icons+Round\" data-smp-icon=\"location_on\"></span>    " + obituary.defunct?.deathCity + "</td></tr>";
33                    });
34
35                    string += "</tbody></table></div>";
36                    $(".autocpl").remove();
37                    inputName.after(string);
38                    $(".clickable tr").click(function () {
39                        var href = $(this).attr("data-href");
40                        if (href) {
41                            window.location = href;
42                        }
43                    });
44                });
45            }, 150);
46        }
47    });
48});
49
50
51(function () {
52    function initCityAutocomplete() {
53        const cityInput = document.getElementById('form-city');
54        if (!cityInput || typeof smpSearchBar === "undefined") {
55            return;
56        }
57        const cityOuput = document.createElement("div")
58        cityOuput.classList.add('city-autocompleted');
59        cityInput.insertAdjacentElement("afterend", cityOuput);
60        const cityForm = document.querySelector("#city form");
61        if (!cityForm) {
62            return;
63        }
64        const urlVille = window.smpDecode(smpSearchBar.urlVille);
65        const lastRes = {input: "", res: undefined };
66        cityForm.addEventListener("submit", async(e) => {
67            e.preventDefault();
68            const currentValue = cityInput.value;
69            let url;
70            if (lastRes.input === currentValue) {
71                url = buildUrl(lastRes.res[0]);
72            } else {
73                const res = await callGeoAPI(currentValue);
74                url = buildUrl(res[0]);
75            }
76            window.location.href = url
77        });
78        const debounce = (callback, milliseconds) => {
79            let timeout;
80
81            return (argument) => {
82                clearTimeout(timeout);
83                timeout = setTimeout(() => callback(argument), milliseconds);
84            };
85        };
86
87        const onKeyup = async (e) => {
88            const inputValue = e.target.value;
89            let res = await callGeoAPI(inputValue);
90            if (e.key === "Enter") {
91                let url;
92                url = buildUrl(res[0]);
93                window.location.href = url
94            }
95            if (inputValue.len < 2) {
96                return;
97            }
98            const htmlCities = res.map((city) => {
99                const regEx = new RegExp(inputValue, "i")
100                let embedName = city.nom.replace(regEx, "<em>" + inputValue + "</em>");
101
102                return `<a href="${buildUrl(city)}">${embedName} <span>${city.departement.nom}</span></a>`
103            })
104            cityOuput.innerHTML = htmlCities.join('')
105        };
106
107        const callGeoAPI = async (inputValue) => {
108            let res;
109            try {
110                const rep = await fetch(`https://geo.api.gouv.fr/communes?nom=` + inputValue +
111                    `&fields=departement&boost=population&limit=5`)
112                res = await rep.json()
113                return res
114            } catch (e) {
115                console.error('ERROR when call api.gouv when get commune')
116                console.error(e)
117                throw Error('ERROR api.gouv -> ', e.message);
118            }
119        }
120
121        cityInput.addEventListener("keyup", debounce(onKeyup, 5));
122
123        function buildUrl(city) {
124            let departmentCode = city.departement.code;
125            if (departmentCode === "2A" || departmentCode === "2B") {
126                departmentCode = "20"
127            }
128            return "/" + urlVille + "/" + slugify(city.nom) + '-' + departmentCode;
129        }
130
131        function slugify(str) {
132            str = str.replace("'", "-"); // replace  ' to -
133            str = str.replace(/^\s+|\s+$/g, ''); // trim
134            str = str.toLowerCase();
135
136            // remove accents, swap ñ for n, etc
137            var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
138            var to = "aaaaeeeeiiiioooouuuunc------";
139            for (var i = 0, l = from.length; i < l; i++) {
140                str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
141            }
142
143            str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
144                .replace(/\s+/g, '-') // collapse whitespace and replace by -
145                .replace(/-+/g, '-'); // collapse dashes
146
147            return str;
148        }
149    }
150
151    if (document.readyState === "loading") {
152        document.addEventListener("DOMContentLoaded", initCityAutocomplete);
153    } else {
154        initCityAutocomplete();
155    }
156})();
157
158
159function autocomplete(input, url, callback) {
160    $.get(url + "\"" + input.val() + "\"", {}, callback);
161}
162
163$(document).mouseup(function (e) {
164    var container = $(".autocpl");
165
166    // if the target of the click isn't the container nor a descendant of the container
167    if (!container.is(e.target) && container.has(e.target).length === 0) {
168        container.hide();
169    }
170});

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.