import { element_default } from "./chunk-S3QBQTEW.js"; import "./chunk-I4Q72WOW.js"; import "./chunk-RTVPCGIJ.js"; import "./chunk-MSWSBYBR.js"; import "./chunk-QCJTGAWF.js"; import "./chunk-CAVOO5JW.js"; import { Control_default } from "./chunk-VRTURNK3.js"; import "./chunk-2C73OZ6M.js"; import "./chunk-M5TTSD4C.js"; import "./chunk-ZCRXKB7J.js"; import "./chunk-RW3V7S4F.js"; import "./chunk-PAB2HIXK.js"; import "./chunk-I6K7MRGV.js"; import "./chunk-PGWX4545.js"; import "./chunk-AYBYZSAV.js"; import "./chunk-YLJGUH5Z.js"; import "./chunk-AZGMK675.js"; import "./chunk-BHVDQB66.js"; import "./chunk-6EWLK2BW.js"; import "./chunk-5D2XPBR2.js"; import "./chunk-SHUBVYN4.js"; import "./chunk-FM44FOIC.js"; import "./chunk-LMC3RO5P.js"; import "./chunk-X52LGBOS.js"; import "./chunk-QFCIXVZ3.js"; import { transform } from "./chunk-A3RXLHYB.js"; import "./chunk-ZLPTRF2L.js"; import "./chunk-54BTDBAD.js"; import "./chunk-UPTVWZ45.js"; import "./chunk-5XHD7RSF.js"; import { Object_default } from "./chunk-Q5ZULJHM.js"; import { unByKey } from "./chunk-NGFXCWUF.js"; import "./chunk-K25ZO44T.js"; import "./chunk-SRXHWJOY.js"; import "./chunk-5RHQVMYD.js"; import "./chunk-DC5AMYBS.js"; // node_modules/ol-ext/control/Search.js var ol_control_Search = class olcontrolSearch extends Control_default { constructor(options) { options = options || {}; var classNames = (options.className || "") + " ol-search" + (options.target ? "" : " ol-unselectable ol-control"); var element = element_default.create("DIV", { className: classNames }); super({ element, target: options.target }); var self = this; if (options.typing == void 0) { options.typing = 300; } this._classname = options.className || "search"; if (options.collapsed !== false) element.classList.add("ol-collapsed"); if (!options.target) { this.button = document.createElement("button"); this.button.setAttribute("type", "button"); this.button.setAttribute("title", options.title || options.label || "Search"); this.button.addEventListener("click", function() { element.classList.toggle("ol-collapsed"); if (!element.classList.contains("ol-collapsed")) { element.querySelector("input.search").focus(); var listElements = element.querySelectorAll("li"); for (var i = 0; i < listElements.length; i++) { listElements[i].classList.remove("select"); } if (!input.value) { self.drawList_(); } } }); element.appendChild(this.button); } if (options.inputLabel) { var label = document.createElement("LABEL"); label.innerText = options.inputLabel; element.appendChild(label); } var tout, cur = ""; var input = this._input = document.createElement("INPUT"); input.setAttribute("type", "search"); input.setAttribute("class", "search"); input.setAttribute("autocomplete", "off"); input.setAttribute("placeholder", options.placeholder || "Search..."); input.addEventListener("change", function(e) { self.dispatchEvent({ type: "change:input", input: e, value: input.value }); }); var doSearch = function(e) { var li = element.querySelector("ul.autocomplete li.select"); var val = input.value; if (e.key == "ArrowDown" || e.key == "ArrowUp" || e.key == "Down" || e.key == "Up") { if (li) { var newli = /Down/.test(e.key) ? li.nextElementSibling : li.previousElementSibling; if (newli && !newli.classList.contains("copy")) { li.classList.remove("select"); newli.classList.add("select"); input.value = newli.innerText; } } else { li = element.querySelector("ul.autocomplete li"); if (li) { li.classList.add("select"); input.value = li.innerText; } } } else if (e.type == "input") { if (!val) { setTimeout(function() { self.drawList_(); }, 200); } if (li) { input.value = val = ""; li.classList.remove("select"); } } else if (li && (e.type === "search" || e.key === "Enter")) { if (element.classList.contains("ol-control")) { input.blur(); } li.classList.remove("select"); cur = val; self._handleSelect(self._list[li.getAttribute("data-search")]); } else if (e.type === "search" || e.key === "Enter" || cur != val && options.typing >= 0) { cur = val; if (cur) { if (tout) { clearTimeout(tout); } var minLength = self.get("minLength"); tout = setTimeout(function() { if (cur.length >= minLength) { var s = self.autocomplete(cur, function(auto) { self.drawList_(auto); }); if (s) { self.drawList_(s); } } else { self.drawList_(); } }, options.typing); } else { self.drawList_(); } } else { li = element.querySelector("ul.autocomplete li"); if (li) li.classList.remove("select"); } }; input.addEventListener("keyup", doSearch); input.addEventListener("search", doSearch); input.addEventListener("cut", doSearch); input.addEventListener("paste", doSearch); input.addEventListener("input", doSearch); if (!options.noCollapse) { input.addEventListener("blur", (function() { setTimeout((function() { if (input !== document.activeElement) { element.classList.add("ol-collapsed"); this.set("reverse", false); element.classList.remove("ol-revers"); } }).bind(this), 200); }).bind(this)); input.addEventListener("focus", (function() { if (!this.get("reverse")) { element.classList.remove("ol-collapsed"); element.classList.remove("ol-revers"); } }).bind(this)); input.addEventListener("keydown", (function() { this.set("reverse", false); element.classList.remove("ol-collapsed"); element.classList.remove("ol-revers"); }).bind(this)); } element.appendChild(input); if (options.reverse) { var reverse = element_default.create("BUTTON", { type: "button", class: "ol-revers", title: options.reverseTitle || "click on the map", on: { focus: (function() { if (!this.get("reverse")) { this.set("reverse", !this.get("reverse")); input.focus(); element.classList.add("ol-revers"); } else { this.set("reverse", false); } }).bind(this) } }); element.appendChild(reverse); } var ul = document.createElement("ul"); ul.classList.add("autocomplete"); element.appendChild(ul); if (typeof options.getTitle == "function") this.getTitle = options.getTitle; if (typeof options.autocomplete == "function") this.autocomplete = options.autocomplete; this.set("copy", options.copy); this.set("minLength", options.minLength || 1); this.set("maxItems", options.maxItems || 10); this.set("maxHistory", options.maxHistory || options.maxItems || 10); if (options.onselect) this.on("select", options.onselect); if (options.centerOnSelect) { this.on("select", (function(e) { var map = this.getMap(); if (map) { map.getView().setCenter(e.coordinate); } }).bind(this)); } if (options.zoomOnSelect) { this.on("select", (function(e) { var map = this.getMap(); if (map) { map.getView().setCenter(e.coordinate); if (map.getView().getZoom() < options.zoomOnSelect) map.getView().setZoom(options.zoomOnSelect); } }).bind(this)); } this.restoreHistory(); this.drawList_(); } /** * Remove the control from its current map and attach it to the new map. * Subclasses may set up event handlers to get notified about changes to * the map here. * @param {ol.Map} map Map. * @api stable */ setMap(map) { if (this._listener) unByKey(this._listener); this._listener = null; super.setMap(map); if (map) { this._listener = map.on("click", this._handleClick.bind(this)); } } /** Collapse the search * @param {boolean} [b=true] * @api */ collapse(b) { if (b === false) this.element.classList.remove("ol-collapsed"); else this.element.classList.add("ol-collapsed"); } /** Get the input field * @return {Element} * @api */ getInputField() { return this._input; } /** Returns the text to be displayed in the menu * @param {any} f feature to be displayed * @return {string} the text to be displayed in the index, default f.name * @api */ getTitle(f) { return f.name || "No title"; } /** Returns title as text * @param {any} f feature to be displayed * @return {string} * @api */ _getTitleTxt(f) { return element_default.create("DIV", { html: this.getTitle(f) }).innerText; } /** Force search to refresh */ search() { var search = this.element.querySelector("input.search"); this._triggerCustomEvent("search", search); } /** Reverse geocode * @param {Object} event * @param {ol.coordinate} event.coordinate * @private */ _handleClick(e) { if (this.get("reverse")) { document.activeElement.blur(); this.reverseGeocode(e.coordinate); } } /** Reverse geocode * @param {ol.coordinate} coord * @param {function | undefined} cback a callback function, default trigger a select event * @api */ reverseGeocode() { } /** Trigger custom event on elemebt * @param {*} eventName * @param {*} element * @private */ _triggerCustomEvent(eventName, element) { element_default.dispatchEvent(eventName, element); } /** Set the input value in the form (for initialisation purpose) * @param {string} value * @param {boolean} search to start a search * @api */ setInput(value, search) { var input = this.element.querySelector("input.search"); input.value = value; if (search) this._triggerCustomEvent("keyup", input); } /** A line has been clicked in the menu > dispatch event * @param {any} f the feature, as passed in the autocomplete * @param {boolean} reverse true if reverse geocode * @param {ol.coordinate} coord * @param {*} options options passed to the event * @api */ select(f, reverse, coord, options) { var event = { type: "select", search: f, reverse: !!reverse, coordinate: coord }; if (options) { for (var i in options) { event[i] = options[i]; } } this.dispatchEvent(event); } /** * Save history and select * @param {*} f * @param {boolean} reverse true if reverse geocode * @param {*} options options send in the event * @private */ _handleSelect(f, reverse, options) { if (!f) return; var hist = this.get("history"); var i; try { var fstr = JSON.stringify(f); for (i = hist.length - 1; i >= 0; i--) { if (!hist[i] || JSON.stringify(hist[i]) === fstr) { hist.splice(i, 1); } } } catch (e) { for (i = hist.length - 1; i >= 0; i--) { if (hist[i] === f) { hist.splice(i, 1); } } } hist.unshift(f); var size = Math.max(0, this.get("maxHistory") || 10) || 0; while (hist.length > size) { hist.pop(); } this.saveHistory(); this.select(f, reverse, null, options); if (reverse) { this.setInput(this._getTitleTxt(f)); this.drawList_(); setTimeout((function() { this.collapse(false); }).bind(this), 300); } } /** Save history (in the localstorage) */ saveHistory() { try { if (this.get("maxHistory") >= 0) { localStorage["ol@search-" + this._classname] = JSON.stringify(this.get("history")); } else { localStorage.removeItem("ol@search-" + this._classname); } } catch (e) { console.warn("Failed to access localStorage..."); } } /** Restore history (from the localstorage) */ restoreHistory() { if (this._history[this._classname]) { this.set("history", this._history[this._classname]); } else { try { this._history[this._classname] = JSON.parse(localStorage["ol@search-" + this._classname]); this.set("history", this._history[this._classname]); } catch (e) { this.set("history", []); } } } /** * Remove previous history */ clearHistory() { this.set("history", []); this.saveHistory(); this.drawList_(); } /** * Get history table */ getHistory() { return this.get("history"); } /** Autocomplete function * @param {string} s search string * @param {function} cback a callback function that takes an array to display in the autocomplete field (for asynchronous search) * @return {Array|false} an array of search solutions or false if the array is send with the cback argument (asnchronous) * @api */ autocomplete(s, cback) { cback([]); return false; } /** Draw the list * @param {Array} auto an array of search result * @private */ drawList_(auto) { var self = this; var ul = this.element.querySelector("ul.autocomplete"); ul.innerHTML = ""; this._list = []; if (!auto) { var input = this.element.querySelector("input.search"); var value = input.value; if (!value) { auto = this.get("history"); } else { return; } ul.setAttribute("class", "autocomplete history"); } else { ul.setAttribute("class", "autocomplete"); } var li, max = Math.min(self.get("maxItems"), auto.length); for (var i = 0; i < max; i++) { if (auto[i]) { if (!i || !self.equalFeatures(auto[i], auto[i - 1])) { li = document.createElement("LI"); li.setAttribute("data-search", this._list.length); this._list.push(auto[i]); li.addEventListener("click", function(e) { self._handleSelect(self._list[e.currentTarget.getAttribute("data-search")]); }); var title = self.getTitle(auto[i]); if (title instanceof Element) li.appendChild(title); else li.innerHTML = title; ul.appendChild(li); } } } if (max && this.get("copy")) { li = document.createElement("LI"); li.classList.add("copy"); li.innerHTML = this.get("copy"); ul.appendChild(li); } } /** Test if 2 features are equal * @param {any} f1 * @param {any} f2 * @return {boolean} */ equalFeatures() { return false; } }; ol_control_Search.prototype._history = {}; var Search_default = ol_control_Search; // node_modules/ol-ext/util/Ajax.js var ol_ext_Ajax = class olextAjax extends Object_default { constructor(options) { options = options || {}; super(); this._auth = options.auth; this.set("dataType", options.dataType || "JSON"); } /** Helper for get * @param {*} options * @param {string} options.url * @param {string} options.auth Authorisation as btoa("username:password"); * @param {string} options.dataType The type of data that you're expecting back from the server, default JSON * @param {string} options.success * @param {string} options.error * @param {*} options.options get options */ static get(options) { var ajax = new ol_ext_Ajax(options); if (options.success) ajax.on("success", function(e) { options.success(e.response, e); }); if (options.error) ajax.on("error", function(e) { options.error(e); }); ajax.send(options.url, options.data, options.options); } /** Helper to get cors header * @param {string} url * @param {string} callback */ static getCORS(url, callback) { var request = new XMLHttpRequest(); request.open("GET", url, true); request.send(); request.onreadystatechange = function() { if (this.readyState == this.HEADERS_RECEIVED) { callback(request.getResponseHeader("Access-Control-Allow-Origin")); } }; } /** Send an ajax request (GET) * @fires success * @fires error * @param {string} url * @param {*} data Data to send to the server as key / value * @param {*} options a set of options that are returned in the * @param {boolean} options.abort false to prevent aborting the current request, default true */ send(url, data, options) { options = options || {}; var self = this; var encode = options.encode !== false; if (encode) url = encodeURI(url); var parameters = ""; for (var index in data) { if (data.hasOwnProperty(index) && data[index] !== void 0) { parameters += (parameters ? "&" : "?") + index + "=" + (encode ? encodeURIComponent(data[index]) : data[index]); } } if (this._request && options.abort !== false) { this._request.abort(); } var ajax = this._request = new XMLHttpRequest(); ajax.open("GET", url + parameters, true); if (options.timeout) ajax.timeout = options.timeout; if (this._auth) { ajax.setRequestHeader("Authorization", "Basic " + this._auth); } this.dispatchEvent({ type: "loadstart" }); ajax.onload = function() { self._request = null; self.dispatchEvent({ type: "loadend" }); if (this.status >= 200 && this.status < 400) { var response; try { switch (self.get("dataType")) { case "JSON": { response = JSON.parse(this.response); break; } default: { response = this.response; } } } catch (e) { self.dispatchEvent({ type: "error", status: 0, statusText: "parsererror", error: e, options, jqXHR: this }); return; } self.dispatchEvent({ type: "success", response, status: this.status, statusText: this.statusText, options, jqXHR: this }); } else { self.dispatchEvent({ type: "error", status: this.status, statusText: this.statusText, options, jqXHR: this }); } }; ajax.ontimeout = function() { self._request = null; self.dispatchEvent({ type: "loadend" }); self.dispatchEvent({ type: "error", status: this.status, statusText: "Timeout", options, jqXHR: this }); }; ajax.onerror = function() { self._request = null; self.dispatchEvent({ type: "loadend" }); self.dispatchEvent({ type: "error", status: this.status, statusText: this.statusText, options, jqXHR: this }); }; ajax.send(); } }; var Ajax_default = ol_ext_Ajax; // node_modules/ol-ext/control/SearchJSON.js var ol_control_SearchJSON = class olcontrolSearchJSON extends Search_default { constructor(options) { options = options || {}; options.className = options.className || "JSON"; delete options.autocomplete; options.minLength = options.minLength || 3; options.typing = options.typing || 800; super(options); var url = options.url || ""; if (window.location.protocol === "https:") { var parser = document.createElement("a"); parser.href = url; parser.protocol = window.location.protocol; url = parser.href; } this.set("url", url); this._ajax = new Ajax_default({ dataType: "JSON", auth: options.authentication }); this._ajax.on("success", (function(resp) { if (resp.status >= 200 && resp.status < 400) { if (typeof this._callback === "function") this._callback(resp.response); } else { if (typeof this._callback === "function") this._callback(false, "error"); console.log("AJAX ERROR", arguments); } }).bind(this)); this._ajax.on("error", (function() { if (typeof this._callback === "function") this._callback(false, "error"); console.log("AJAX ERROR", arguments); }).bind(this)); this._ajax.on("loadstart", (function() { this.element.classList.add("searching"); }).bind(this)); this._ajax.on("loadend", (function() { this.element.classList.remove("searching"); }).bind(this)); if (typeof options.handleResponse === "function") this.handleResponse = options.handleResponse; } /** Send ajax request * @param {string} url * @param {*} data * @param {function} cback a callback function that takes an array of {name, feature} to display in the autocomplete field */ ajax(url, data, cback, options) { options = options || {}; this._callback = cback; this._ajax.set("dataType", options.dataType || "JSON"); this._ajax.send(url, data, options); } /** Autocomplete function (ajax request to the server) * @param {string} s search string * @param {function} cback a callback function that takes an array of {name, feature} to display in the autocomplete field */ autocomplete(s, cback) { var data = this.requestData(s); var url = encodeURI(this.get("url")); this.ajax(url, data, function(resp) { if (typeof cback === "function") cback(this.handleResponse(resp)); }); } /** * @param {string} s the search string * @return {Object} request data (as key:value) * @api */ requestData(s) { return { q: s }; } /** * Handle server response to pass the features array to the display list * @param {any} response server response * @return {Array} an array of feature * @api */ handleResponse(response) { return response; } }; var SearchJSON_default = ol_control_SearchJSON; // node_modules/ol-ext/control/SearchNominatim.js var ol_control_SearchNominatim = class olcontrolSearchNominatim extends SearchJSON_default { constructor(options) { options = options || {}; options.className = options.className || "nominatim"; options.typing = options.typing || -1; options.url = options.url || "https://nominatim.openstreetmap.org/search"; options.copy = '© OpenStreetMap contributors'; super(options); this.set("polygon", options.polygon); this.set("viewbox", options.viewbox); this.set("bounded", options.bounded); } /** Returns the text to be displayed in the menu * @param {ol.Feature} f the feature * @return {string} the text to be displayed in the index * @api */ getTitle(f) { var info = []; if (f.class) info.push(f.class); if (f.type) info.push(f.type); var title = f.display_name + (info.length ? "" + info.join(" - ") + "" : ""); if (f.icon) title = "" + title; return title; } /** * @param {string} s the search string * @return {Object} request data (as key:value) * @api */ requestData(s) { var data = { format: "json", addressdetails: 1, q: s, polygon_geojson: this.get("polygon") ? 1 : 0, bounded: this.get("bounded") ? 1 : 0, limit: this.get("maxItems") }; if (this.get("viewbox")) data.viewbox = this.get("viewbox"); return data; } /** A ligne has been clicked in the menu > dispatch event * @param {any} f the feature, as passed in the autocomplete * @api */ select(f) { var c = [Number(f.lon), Number(f.lat)]; try { c = transform(c, "EPSG:4326", this.getMap().getView().getProjection()); } catch (e) { } this.dispatchEvent({ type: "select", search: f, coordinate: c }); } /** Reverse geocode * @param {ol.coordinate} coord * @api */ reverseGeocode(coord, cback) { var lonlat = transform(coord, this.getMap().getView().getProjection(), "EPSG:4326"); this.ajax( this.get("url").replace("search", "reverse"), { lon: lonlat[0], lat: lonlat[1], format: "json" }, (function(resp) { if (cback) { cback.call(this, [resp]); } else { if (resp && !resp.error) { this._handleSelect(resp, true); } } }).bind(this) ); } /** * Handle server response to pass the features array to the display list * @param {any} response server response * @return {Array} an array of feature * @api */ handleResponse(response) { return response.results || response; } /**/ }; var SearchNominatim_default = ol_control_SearchNominatim; export { SearchNominatim_default as default }; //# sourceMappingURL=ol-ext_control_SearchNominatim.js.map