var JS_PHP_PROVIDER_URL='ebcbgdapi';function formatLocation(lat, lng) { return lat.toFixed(7) + (lat > 0 ? 'N' : 'S') + ' ' + lng.toFixed(7) + (lng > 0 ? 'E' : 'W'); } const C_AJAX_PROXY_ERR_RESEND_COUNT = 2; function ajaxEbcSearchCb(xmlhttp, ok_cb) { try { /* readyState Holds the status of the XMLHttpRequest. 0: request not initialized 1: server connection established 2: request received 3: processing request 4: request finished and response is ready */ if (xmlhttp.readyState == xmlhttp.DONE) { xmlhttp.t.setXObj.call(xmlhttp.t, xmlhttp.xobj_sel, null); if (xmlhttp.status == 200) { if (ok_cb) { console.log('ajaxEbcSearchCb'); ok_cb(xmlhttp); } } else if (xmlhttp.status == 502) { EbcLogger.d('Resending ajax rq on status ' + xmlhttp.status + 'resendCounter: ' + xmlhttp.resendCounter, this, 'ajaxEbcSearchCb'); if (xmlhttp.resendCounter != null && xmlhttp.resendCounter <= C_AJAX_PROXY_ERR_RESEND_COUNT && xmlhttp.resendCb) { xmlhttp.resendCb(xmlhttp); } } /* else console.log("Chyba AJAX " + xmlhttp.status); */ progress.hideProgress(); } } catch (e) { console.log(e); console.log(xmlhttp.responseText); progress.hideProgress(); } } function ajaxEbcSearchTimeoutCb(xmlhttp) { try { progress.hideProgress(); } catch (e) { console.log(e); console.log(xmlhttp.responseText); } } const SEARCH_MAX_ITEMS = 20; function EbcSearch() { return { xmlhttp_search: null, xmlhttp_nearest_city: null, xmlhttp_nearest_chargers: null, options: { PARAM_COMMAND: "c", PARAM_COORD_X: "x", PARAM_COORD_Y: "y", PARAM_COORD_Z: "z", PARAM_SEARCH_TEXT: "t", PARAM_SEARCH_LATITUDE: "lat", PARAM_SEARCH_LONGITUDE: "lon", PARAM_SEARCH_FIND_PARAM: "fpar", PARAM_SEARCH_MARKER_TYPE: "marker_type", COMMAND_SEARCH_TEXT: "st", COMMAND_SEARCH_CITY: "sc", COMMAND_SEARCH_NEAREST: "snc", COMMAND_SEARCH_NEAREST_CITY: "snct", PHP_PROVIDER_URL: JS_PHP_PROVIDER_URL, X_SEL_SEARCH: 1, X_SEL_NEAREST_CITY: 2, X_SEL_NEAREST_CHARGERS: 3, }, setXObj(sel, val) { switch (sel) { case this.options.X_SEL_SEARCH: this.xmlhttp_search = val; break; case this.options.X_SEL_NEAREST_CITY: this.xmlhttp_nearest_city = val; break; case this.options.X_SEL_NEAREST_CHARGERS: this.xmlhttp_nearest_chargers = val; break; } }, getXObj(sel) { switch (sel) { case this.options.X_SEL_SEARCH: return this.xmlhttp_search; case this.options.X_SEL_NEAREST_CITY: return this.xmlhttp_nearest_city; case this.options.X_SEL_NEAREST_CHARGERS: return this.xmlhttp_nearest_chargers; default: return null; } }, initialize: function (cb, nearest_cb) { // (String) this._cb = cb; this._nearest_cb = nearest_cb; log('initialize'); }, _ajaxSendData: function (cb, url, params, xobj_sel, abort = true, direct = false, resendCounter = null) { if ((params) && (params.length > 0)) { if (abort && this.getXObj(xobj_sel)) { log('_ajaxSendData/abort...'); try { this.getXObj(xobj_sel).abort(); log('_ajaxSendData/abort ok'); } catch (error) { log('_ajaxSendData/abort error'); } } if (window.XMLHttpRequest) { this.setXObj(xobj_sel, new XMLHttpRequest()); } else if (window.ActiveXObject) { try { this.setXObj(xobj_sel, new ActiveXObject("Msxml2.XMLHTTP")); } catch (error) { this.setXObj(xobj_sel, new ActiveXObject("Microsoft.XMLHTTP")); } } this.getXObj(xobj_sel).t = this; if (!this.getXObj(xobj_sel)) { log('_ajaxSendData/xmlhttp=null'); return false; } let xmlhttp = this.getXObj(xobj_sel); // this.getXObj(xobj_sel).open("GET", url + '?' + params, true); xmlhttp.open("POST", url, true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.onreadystatechange = function() { if (xmlhttp.t.getXObj(xobj_sel) == xmlhttp) xmlhttp.t.setXObj(xobj_sel, null); ajaxEbcSearchCb( xmlhttp, cb ); }; xmlhttp.ontimeout = function() { if (xmlhttp.t.getXObj(xobj_sel) == xmlhttp) xmlhttp.t.setXObj(xobj_sel, null); log('_ajaxSendData/ontimeout'); ajaxEbcSearchTimeoutCb( xmlhttp ); }; xmlhttp.t = this; xmlhttp.cb = cb; xmlhttp.url = url; xmlhttp.params = params; xmlhttp.xobj_sel = xobj_sel; xmlhttp.abort_flg = abort; xmlhttp.direct = direct; xmlhttp.resendCounter = resendCounter == null ? 1 : resendCounter + 1; xmlhttp.resendCb = function (xmlhttp) { xmlhttp.t._ajaxSendData.call(xmlhttp.t, cb, url, params, xobj_sel, abort, direct, resendCounter + 1) }; if (direct) { log('_ajaxSendData/direct send'); xmlhttp.send(params); } else { setTimeout((x) => { if (x == xmlhttp.t.getXObj(xobj_sel)) { log('_ajaxSendData/delay send'); xmlhttp.t.getXObj(xobj_sel).send(params); } else { ajaxEbcSearchTimeoutCb( x ); } }, 200, xmlhttp); } progress.showProgress(); } else return false; }, _ajaxFormatParameter: function (name, value) { return encodeURIComponent(name) + "=" + encodeURIComponent(value); }, _ajaxFormatParameters: function (params_arr) { let res = params_arr.length == 0 ? "" : params_arr[0]; let i; for (i = 1; i < params_arr.length; i++) { res = res + "&" + params_arr[i]; } return res; }, sendSearchCityRq: function(text) { // city name let params = this._ajaxFormatParameters( [ this._ajaxFormatParameter(this.options.PARAM_COMMAND, this.options.COMMAND_SEARCH_CITY), this._ajaxFormatParameter(this.options.PARAM_SEARCH_TEXT, text) ] ); this._ajaxSendData(this._cb, this.options.PHP_PROVIDER_URL, params, this.options.X_SEL_SEARCH); //this._ajaxSendData(this._cb, 'https://nominatim.openstreetmap.org/search', 'city=' + text + '&format=json'); log('sendSearchCityRq (' + text + ')'); }, sendSearchNearestChargers: function(lat, lon, marker_type) { let params = this._ajaxFormatParameters( [ this._ajaxFormatParameter(this.options.PARAM_COMMAND, this.options.COMMAND_SEARCH_NEAREST), this._ajaxFormatParameter(this.options.PARAM_SEARCH_LATITUDE, lat), this._ajaxFormatParameter(this.options.PARAM_SEARCH_LONGITUDE, lon), this._ajaxFormatParameter(this.options.PARAM_SEARCH_MARKER_TYPE, marker_type) ] ); this._ajaxSendData(this._nearest_cb, this.options.PHP_PROVIDER_URL, params, this.options.X_SEL_NEAREST_CHARGERS, true); log('sendSearchNearestChargers (' + lat + ', ' + lon + ')'); }, sendSearchNearestCity: function(lat, lon, cb, max = SEARCH_MAX_ITEMS) { let params = this._ajaxFormatParameters( [ this._ajaxFormatParameter(this.options.PARAM_COMMAND, this.options.COMMAND_SEARCH_NEAREST_CITY), this._ajaxFormatParameter(this.options.PARAM_SEARCH_LATITUDE, lat), this._ajaxFormatParameter(this.options.PARAM_SEARCH_LONGITUDE, lon), this._ajaxFormatParameter(this.options.PARAM_SEARCH_FIND_PARAM, max) ] ); this._ajaxSendData(cb, this.options.PHP_PROVIDER_URL, params, this.options.X_SEL_NEAREST_CITY, false, true); log('sendSearchNearestCity (' + lat + ', ' + lon + ')'); }, } }