1/**
2 * @license    http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.html
3 * @author     Francois Merciol <dokuplugin@merciol.fr>
4 *
5 * INSEE city: code database
6 *
7 * Sources :
8 *   https://www.insee.fr/fr/information/4316069
9 *   http://www.geonames.org/export/codes.html
10 *   http://download.geonames.org/export/dump/
11 */
12
13var inseeCityDep;
14var inseeCityName;
15var inseeCityNameInsee;
16var inseeCityNameLatLon;
17var inseeCityNameLatLonDep;
18if (inseeCityNameLatLonDep === undefined)
19    inseeCityNameLatLonDep = new Map ();
20
21jQuery (function () {
22    inseeCityDep = JSINFO['inseeCity']['selectedDeps'];
23    inseeCityMaxAutocomplete = JSINFO['inseeCity']['maxAutocomplete'];
24    inseeCityName = new Array ();
25    inseeCityNameInsee = new Array ();
26    inseeCityNameLatLon = new Map ();
27
28    inseeCityDep.forEach ( (dep, index) => {
29	for (var cityCode in inseeCityNameLatLonDep[dep]) {
30	    inseeCityNameLatLon [cityCode] = inseeCityNameLatLonDep[dep][cityCode];
31	    inseeCityName.push (inseeCityNameLatLon[cityCode][0]+" ("+cityCode+")");
32	    inseeCityNameInsee [inseeCityNameLatLon[cityCode][0].toLowerCase ()] = cityCode;
33	}
34    });
35    inseeCityName.sort ();
36
37    /* autocomplete */
38    jQuery ('.insee input[name="city"]').autocomplete ({
39	source: inseeCityName,
40	source: function (request, response) {
41	    var upper = request.term.toUpperCase ();
42            var results = jQuery.ui.autocomplete.filter (inseeCityName, request.term);
43	    var startsWith = [];
44	    jQuery.each (results, function (i, v) {
45	    	if (v.toUpperCase ().indexOf (upper) == 0)
46	    	    startsWith.push (v);
47	    });
48	    results = results.slice (0, inseeCityMaxAutocomplete);
49	    jQuery.each (results, function (i, v) {
50	    	if (v.toUpperCase ().indexOf (upper) != 0)
51	    	    startsWith.push (v);
52	    });
53	    response (startsWith.slice (0, inseeCityMaxAutocomplete));
54	}
55    });
56});
57
58