1 2if (window.frameCacheVars !== undefined) { 3 BX.addCustomEvent("onFrameDataReceived", function (json) { 4 window.BXmakerGeoIPCity.init($('.js-bxmaker__geoip__city')); 5 }); 6} else { 7 BX.ready(function () { 8 window.BXmakerGeoIPCity.init($('.js-bxmaker__geoip__city')); 9 }); 10} 11 12 13if (!!window.BXmakerGeoIPCity === false) { 14 15 (function () { 16 /** 17 * Êëàññ äëÿ ðàáîòû ñ êîìîïíåíòîì bxmaker:geoip.city, îòâå÷àåò çà ïîêàç, ñìåíó ãîðîäà 18 * òàêîé êîìîïíåíò íà ñòðàíèöå äîëæåí áûòü 1, åñëè áîëüøå, òî îñòàëüíûå áóäóò ïðîèãíîðèðâîàíû 19 * @constructor 20 */ 21 var BXmakerGeoIPCityConstructor = function () { 22 var that = this; 23 that.params = {}; 24 that.box = null; 25 that.popup = null; 26 that.bInit = false; 27 }; 28 29 /** 30 * Ïîñëå ïîëó÷åíèÿ äàííûõ îò ôîíîâîãî çàïðîñà ïðè êîìïîçèòå, ïðîèñõîäèò ïîäãîòîâêà áëîêà, çàìåíà íàçâàíèÿ ãîðîäà íà àêòóàëüíûé âûçîâ ñîáûòèé è òï 31 * @param box 32 * @returns {boolean} 33 */ 34 BXmakerGeoIPCityConstructor.prototype.init = function (box) { 35 36 var that = this; 37 if (!box.length) return false; 38 if (box.filter('.js-bxmaker__geoip__city--init').length) return false; 39 40 that.params = {}; 41 that.box = box.eq(0); // çäåñü êàê ðàç è ïðîèñõîäèò îòáîð ïåðâîãî áëîêà, îñòàëüíûå èãíîðèðóþòñÿ 42 43 that.box.addClass('js-bxmaker__geoip__city--init'); 44 that.popup = that.box.find('.js-bxmaker__geoip__popup').detach(); 45 that.rand = that.box.attr('data-rand'); 46 47 // ïðîâåðèì, åñòü ëè äàííûå äëÿ ðàáîòû êîìîïíåíòà 48 if(!window.BxmakerGeoipCityData || !window.BxmakerGeoipCityData[that.rand]) 49 { 50 return false; 51 } 52 53 that.params = window.BxmakerGeoipCityData[that.rand]; 54 55 if(typeof(that.getCityId()) == 'undefined') 56 { 57 setTimeout(function(){ 58 that.init(box); 59 },400); 60 return false; 61 } 62 63 // ïðîâåðêà âêëþ÷åíà ëè îòëàäêà âðó÷íóþ 64 if (this.cookie('debug') == 'Y') { 65 that.params.debug = true; 66 } 67 68 that.initEvent(); 69 that.bInit = true; 70 that.showCity(); 71 72 73 74 // óòî÷íåíèå âêëþ÷åíî, ïîèñê âêëþ÷åíî 75 if (that.params.useYandex && that.params.useYandexSearch) { 76 window.BXmakerGeoIPYandexGeo.init(); 77 } 78 // óòî÷íåíèå âêë÷þåíî, ïîèñê âûêë÷þåíî 79 else if(that.params.useYandex && !that.params.useYandexSearch) 80 { 81 // ìåñòîïîëîæåíèå óæå îïðåäåëÿåëîñü , ïîâòîðíî íå íóæíî --- 82 if (that.cookie('yandex_location_defined') == 'Y' && that.cookie('yandex_location_defined_check') == '1') { 83 that.checkRedirect(); 84 } 85 else { 86 window.BXmakerGeoIPYandexGeo.init(); 87 } 88 } 89 //óòî÷íåíåè âûêëþ÷åíî, ïîèñê âêëþ÷åí 90 else if(!that.params.useYandex && that.params.useYandexSearch) 91 { 92 window.BXmakerGeoIPYandexGeo.init(); 93 } 94 // è ïîèñê è óòî÷íåíèå îòêëþ÷åíû 95 else { 96 that.checkRedirect(); 97 } 98 99 }; 100 101 /** 102 * Ïðîâåðêà, èíèöèàëèçèðîâàí ëè êîìïîíåíò 103 * @returns {boolean} 104 */ 105 BXmakerGeoIPCityConstructor.prototype.isInit = function () { 106 return this.bInit; 107 }; 108 109 /** 110 * Âêëþ÷åíèå/îòêë÷þåíèå ðåæèìà îòëàäêè 111 * @param flag 112 */ 113 BXmakerGeoIPCityConstructor.prototype.setDebug = function (flag) { 114 this.params.debug = flag; 115 if(flag) 116 { 117 this.cookie('debug', 'Y'); 118 } 119 else 120 { 121 this.cookie('debug', 'N'); 122 } 123 }; 124 125 126 /** 127 * Ëîã äåéñòâèé ïðè îòëàäêå 128 */ 129 BXmakerGeoIPCityConstructor.prototype.log = function () { 130 if (this.params.debug) { 131 var args = Array.prototype.slice.call(arguments); 132 args.unshift('bxmaker:geoip.city: '); 133 console.log.apply(console, args); 134 } 135 }; 136 137 /** 138 * Ëîã îøèáîê ïðè îòëàäêå 139 */ 140 BXmakerGeoIPCityConstructor.prototype.logError = function () { 141 if (this.params.debug) { 142 var args = Array.prototype.slice.call(arguments); 143 args.unshift('bxmaker:geoip.city: '); 144 console.error.apply(console, args); 145 } 146 }; 147 148 /** 149 * Âåðíåò äîìåí, äëÿ êîòîðîãî óñòàíàëâèâàþòñÿ êóêè 150 * @returns {*} 151 */ 152 BXmakerGeoIPCityConstructor.prototype.getCookieDomain = function () { 153 var that = this; 154 return that.params.cookieDomain; 155 }; 156 157 158 /** 159 * Ïðåîáðàçîâàíèå ê ÷èñëó 160 * @param num 161 * @returns {number} 162 */ 163 BXmakerGeoIPCityConstructor.prototype.intval = function (num) { 164 if (typeof num == 'number' || typeof num == 'string') { 165 num = num.toString(); 166 var dotLocation = num.indexOf('.'); 167 if (dotLocation > 0) { 168 num = num.substr(0, dotLocation); 169 } 170 if (isNaN(Number(num))) { 171 num = parseInt(num); 172 } 173 if (isNaN(num)) { 174 return 0; 175 } 176 return Number(num); 177 } 178 else if (typeof num == 'object' && num.length != null && num.length > 0) {//Íåïóñòîé ìàññèâ/îáúåêò -> 1 179 return 1; 180 } 181 else if (typeof num == 'boolean' && num === true) {//true -> 1 182 return 1; 183 } 184 return 0;//×óòü ÷òî íå òàê - ñðàçó â íîëü 185 }; 186 187 /** 188 * Ïîëó÷åíèå, çàïèñü êóê 189 * @param name 190 * @param value 191 * @param params 192 * @returns {any} 193 */ 194 BXmakerGeoIPCityConstructor.prototype.cookie = function (name, value, params) { 195 var that = this; 196 var d = new Date(); 197 var name = that.params.cookiePrefix + name; 198 var params = params || {}; 199 var parts = []; 200 var currentValue, matches; 201 202 203 if (value === undefined) { 204 matches = document.cookie.match(new RegExp( 205 "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)" 206 )); 207 currentValue = matches ? decodeURIComponent(matches[1].replace(/\+/g, ' ')) : undefined; 208 that.log('cookie get: ' + name + ' = ' + currentValue); 209 return currentValue; 210 } 211 else { 212 value = encodeURIComponent(value); 213 d.setTime(d.getTime() + ((!!params.expires ? params.expires : 365) * 24 * 60 * 60 * 1000)); 214 215 parts.push(name + "=" + value);// todo parts.push(name + "=" + encodeURIComponent(value)); 216 parts.push("expires=" + d.toUTCString()); 217 parts.push("path=" + (!!params.path ? params.path : '/')); 218 parts.push("domain=" + that.getCookieDomain()); 219 220 document.cookie = parts.join('; ');
221 that.log('cookie: ' + parts.join('; '), parts); 222 } 223 }; 224 225 /** 226 * Ïîëó÷åíèå ïàðàìåòðîâ url ñòðàíèöû â ôîðìàòå json äëÿ óäîáñòâà 227 * @param hashBased 228 * @returns {Array} 229 */ 230 BXmakerGeoIPCityConstructor.prototype.getJsonFromUrl = function (hashBased) { 231 var query; 232 if (hashBased) { 233 var pos = location.href.indexOf("?"); 234 if (pos == -1) return []; 235 query = location.href.substr(pos + 1); 236 } else { 237 query = location.search.substr(1); 238 } 239 var result = {}; 240 query.split("&").forEach(function (part) { 241 if (!part) return; 242 part = part.split("+").join(" "); // replace every + with space, regexp-free version 243 var eq = part.indexOf("="); 244 var key = eq > -1 ? part.substr(0, eq) : part; 245 var val = eq > -1 ? decodeURIComponent(part.substr(eq + 1)) : ""; 246 var from = key.indexOf("["); 247 if (from == -1) result[decodeURIComponent(key)] = val; 248 else { 249 var to = key.indexOf("]", from); 250 var index = decodeURIComponent(key.substring(from + 1, to)); 251 key = decodeURIComponent(key.substring(0, from)); 252 if (!result[key]) result[key] = []; 253 if (!index) result[key].push(val); 254 else result[key][index] = val; 255 } 256 }); 257 return result; 258 }; 259 260 /** 261 * Èíèöèàëèçàöè ñîáûòèé, äëÿ îòñëåæèâàíèÿ êëèêîâ, íàáîðà òåêñòà è òï 262 */ 263 BXmakerGeoIPCityConstructor.prototype.initEvent = function () { 264 var that = this; 265 var searchTimeout = false; 266 267 // ïîñëå òîãî êàê ìåñòîïîëîæåíèå áûëî óòî÷íåíî ÷åðåç ÿíäåêñ ------------ 268 $(document).on('bxmaker.geoip.city.yandex.defined', function (event, params) { 269 that.log('save user location defined by yandex'); 270 that.selectLocation(false, params); 271 that.cookie('yandex_location_defined', 'Y'); 272 that.cookie('yandex_location_defined_check', '1'); 273 that.checkRedirect(); 274 }); 275 276 //áèáëèîòåêà ÿíäåêñ êàðò äëÿ ðàáîòû ñ ãåîëîêàöèåé ïîëüâçîâàòåëÿ çàãðóæåíà ------ 277 $(document).on('bxmaker.geoip.yandex.geo.loaded', function () { 278 279 //óòî÷íåíèå ÷åðåç ÿíäåêñ âûêë÷þåíî -- 280 if (!that.params.useYandex) { 281 that.cookie('yandex_location_defined_check', '0'); 282 return true; 283 } 284 285 // ìåñòîïîëîæåíèå óæå îïðåäåëÿåëîñü , ïîâòîðíî íå íóæíî --- 286 if (that.cookie('yandex_location_defined') == 'Y' && that.cookie('yandex_location_defined_check') == '1') { 287 that.checkRedirect(); 288 return true; 289 } 290 291 that.log('Yandex geocoder find user location'); 292 293 window.BXmakerGeoIPYandexGeo.getConnection().geolocation.get({ 294 provider: 'yandex', 295 timeout: 5000, 296 autoReverseGeocode: true 297 }).then(function (res) { 298 299 that.log('Yandex geocoder success', res); 300 301 var boundedBy = res.geoObjects.get(0).properties.get('boundedBy'); 302 var responseItem = res.geoObjects.get(0).properties.get('metaDataProperty').GeocoderMetaData; 303 var responseItemAddress = responseItem.Address.Components; 304 var item = {}; 305 var bSkip = false; 306 307 308 item = { 309 'location': responseItem.id, 310 'city': '', 311 'city_id': responseItem.id, 312 'country': '', 313 'country_id': 0, 314 'region': '', 315 'region_id': 0, 316 'area': '', 317 'zip': '000000', 318 'range': 0, 319 'lng': 0, 320 'lat': 0, 321 'yandex': 1 322 }; 323 324 for (var ai in responseItemAddress) { 325 switch (responseItemAddress[ai].kind) { 326 case 'locality': { 327 item['city'] = responseItemAddress[ai].name; 328 break; 329 } 330 case 'area': {
331 item['area'] = responseItemAddress[ai].name; 332 break; 333 } 334 case 'province': { 335 item['region'] = responseItemAddress[ai].name; 336 break; 337 } 338 case 'country': { 339 item['country'] = responseItemAddress[ai].name; 340 break; 341 } 342 } 343 } 344 345 bSkip = false; 346 for (var ci = 0; ci < that.params.yandexSearchSkipWords.length; ci++) { 347 if (that.params.yandexSearchSkipWords[ci].replace(/^\s+/, '').replace(/\s+$/).length > 0 && item['city'].indexOf(that.params.yandexSearchSkipWords[ci].replace(/^\s+/, '').replace(/\s+$/)) != -1) { 348 that.log('Skip location ' + item['city'] + ': skip word = ' + that.params.yandexSearchSkipWords[ci]);
349 bSkip = true; 350 } 351 } 352 353 item['lng'] = (boundedBy[1][0] - boundedBy[0][0]); 354 item['lat'] = (boundedBy[1][1] - boundedBy[0][1]); 355 356 if (responseItem.kind == 'locality' || (responseItem.kind == 'province' && item['city'] == item['region'])) { 357 if (!bSkip) { 358 that.log('location is defined'); 359 that.log('trigger:bxmaker.geoip.city.yandex.defined', item); 360 $(document).trigger('bxmaker.geoip.city.yandex.defined', item); 361 } 362 } 363 else { 364 that.logError('location is not defined'); 365 } 366 367 368 }, function (e) { 369 that.logError('location is not defined'); 370 }); 371 }); 372 373 $(document).on('bxmaker.geoip.city.search.start', function () { 374 that.popupShow(); 375 }); 376 377 $(document).on('click', '.js-bxmaker__geoip__city-name-global', function () { 378 that.popupShow(); 379 }); 380 381 382 // click po knopke zakryt ocno dlya smeny goroda 383 that.popup.on("click", '.js-bxmaker__geoip__popup-close', function () { 384 that.log('click by close popup btn'); 385 that.popupHide(); 386 }); 387 388 //click po fonu 389 that.popup.on("click", '.js-bxmaker__geoip__popup-background', function () { 390 that.log('click by popup background'); 391 that.popupHide(); 392 }); 393 394 //clear input 395 that.popup.on("click", '.js-bxmaker__geoip__popup-search-clean', function () { 396 if ($(this).hasClass('preloader')) return false; 397 that.log('Search input clean'); 398 that.popup.find('input[name="city"]').val('').focus(); 399 that.popup.find('.js-bxmaker__geoip__popup-search-options').hide(); 400 }); 401 402 that.popup.on("click", '.js-bxmaker__geoip__popup-option', function () { 403 var item = $(this); 404 405 that.popup.find('.js-bxmaker__geoip__popup-search-options').hide(); 406 407 if (that.params.useYandexSearch) { 408 that.popup.find('input[name="city"]').val(item.text()).keyup(); 409 } 410 else { 411 that.selectLocation(item.attr('data-id')); 412 that.popupHide(); 413 } 414 }); 415 416 that.popup.on("keyup", 'input[name="city"]', function () { 417 418 var adres = $(this).val().trim(); 419 420 if (adres.length < 2) return; 421 if (!!searchTimeout) clearTimeout(searchTimeout); 422 423 searchTimeout = setTimeout(function () { 424 425 that.log('Search adress:' + adres); 426 that.popup.find('.js-bxmaker__geoip__popup-search-clean').addClass('preloader'); 427 that.popup.find('.js-bxmaker__geoip__popup-search-search').hide(); 428 429 that.search(adres, function (r) { 430 431 that.log('count search results:' + r.response.count); 432 433 that.popup.find('.js-bxmaker__geoip__popup-search-clean').removeClass('preloader'); 434 435 var searchOptions = that.popup.find('.js-bxmaker__geoip__popup-search-options'); 436 searchOptions.empty(); 437 438 439 if (!!r.response) { 440 if (r.response.count <= 0) { 441 searchOptions.append($('<div class="bxmaker__geoip__popup-search-option bxmaker__geoip__popup-search-option--empty" >' + that.params.messages.emptyResult + '</div>')); 442 } 443 else { 444 for (var i = 0; i < r.response.count; i++) { 445 var item = r.response.items[i]; 446 447 var itemName = ('<span>' + item.city + '</span>' + (!!item.district ? ', ' + item.district : '') + (!!item.region && item.region != item.city ? ', ' + item.region : '') + (!!item.country ? ', ' + item.country : '')); 448 var itemHtml = '<div class="bxmaker__geoip__popup-search-option js-bxmaker__geoip__popup-search-option" '; 449 450 itemHtml += (' data-location="' + item.location + '" '); 451 itemHtml += (' data-city="' + item.city + '" '); 452 itemHtml += (' data-city_id="' + item.city_id + '" '); 453 itemHtml += (' data-country="' + item.country + '" '); 454 itemHtml += (' data-country_id="' + item.country_id + '" '); 455 itemHtml += (' data-region="' + item.region + '" '); 456 itemHtml += (' data-region_id="' + item.region_id + '" '); 457 itemHtml += (' data-area="' + item.area + '" '); 458 itemHtml += (' data-zip="' + item.zip + '" '); 459 itemHtml += (' data-range="' + item.range + '" '); 460 itemHtml += (' data-lng="' + item.lng + '" '); 461 itemHtml += (' data-lat="' + item.lat + '" '); 462 itemHtml += (' data-yandex="' + item.yandex + '" '); 463 itemHtml += ' >'; 464 465 itemHtml += itemName; 466 itemHtml += '</div>'; 467 468 searchOptions.append($(itemHtml)); 469 } 470 } 471 } 472 else if (!!r.error) { 473 searchOptions.append('<div class="bxmaker__geoip__popup-search-option bxmaker__geoip__popup-search-option--empty" >' + r.error.msg + '</div>'); 474 } 475 476 searchOptions.show(); 477 478 479 }, function (r) { 480 that.logError('Error search adress:', r); 481 482 that.popup.find('.js-bxmaker__geoip__popup-search-clean').removeClass('preloader'); 483 that.popup.find('.js-bxmaker__geoip__popup-search-search').empty().append($('<div class="bxmaker__geoip__popup-search-option bxmaker__geoip__popup-search-option--empty" >' + that.params.messages.emptyResult + '</div>')); 484 }); 485 }, that.params.searchTimeout); 486 }); 487 488 that.popup.on("click", '.js-bxmaker__geoip__popup-search-option', function () { 489 var item = $(this); 490 491 that.popup.find('input[name="city"]').val(item.find('span').text()); 492 that.popup.find('.js-bxmaker__geoip__popup-search-options').hide(); 493 494 if (that.params.useYandexSearch) { 495 // ñîõðàíÿåì íà ñåðâåðå 496 that.selectLocation(false, { 497 location: item.attr('data-location'), 498 city: item.attr('data-city'), 499 city_id: item.attr('data-city_id'), 500 region: item.attr('data-region'), 501 region_id: item.attr('data-region_id'), 502 country: item.attr('data-country'), 503 country_id: item.attr('data-country_id'), 504 area: item.attr('data-area'), 505 zip: item.attr('data-zip'), 506 range: item.attr('data-range'), 507 lng: item.attr('data-lng'), 508 lat: item.attr('data-lat'), 509 yandex: item.attr('data-yandex') 510 }); 511 } 512 else { 513 that.selectLocation(item.attr('data-location')); 514 } 515 that.popupHide(); 516 }); 517 }; 518 519 /** 520 * Ñêðûòèå îêíà ñ ïîèñêîì ãîðîäà èëè âûáîðîì èç ñïèñêà èçáðàííûõ 521 */ 522 BXmakerGeoIPCityConstructor.prototype.popupHide = function () { 523 var that = this; 524 that.log('trigger:bxmaker.geoip.popup.hide.before'); 525 $(document).trigger('bxmaker.geoip.popup.hide.before'); 526 527 that.log('popup hide'); 528 that.popup.find('.js-bxmaker__geoip__popup-background').animate({'opacity': '0'}, that.params.animateTimeout); 529 that.popup.find('.js-bxmaker__geoip__popup-content').animate({'opacity': '0'}, { 530 duration: that.params.animateTimeout, 531 complete: function () { 532 that.popup.hide(); 533 that.popup.detach(); 534 that.log('trigger:bxmaker.geoip.popup.hide.after', { 535 'popup': that.popup 536 }); 537 $(document).trigger('bxmaker.geoip.popup.hide.after', { 538 'popup': that.popup 539 }); 540 } 541 }); 542 }; 543 544 /** 545 * Ïîêàç îêíà ñ âûáîðîì, ïîèñêîì ãîðîäà 546 */ 547 BXmakerGeoIPCityConstructor.prototype.popupShow = function () { 548 var that = this; 549 that.log('trigger:bxmaker.geoip.popup.show.before'); 550 $(document).trigger('bxmaker.geoip.popup.show.before'); 551 552 that.log('popup show'); 553 554 $('body').append(that.popup); 555 that.popup.show(); 556 that.popup.find('.js-bxmaker__geoip__popup-background').animate({'opacity': '1'}, that.params.animateTimeout); 557 that.popup.find('.js-bxmaker__geoip__popup-content').animate({'opacity': '1'}, { 558 duration: that.params.animateTimeout, 559 complete: function () { 560 that.log('trigger:bxmaker.geoip.popup.show.after', { 561 'popup': that.popup 562 }); 563 $(document).trigger('bxmaker.geoip.popup.show.after', { 564 'popup': that.popup 565 }); 566 } 567 }); 568 }; 569 570 571 BXmakerGeoIPCityConstructor.prototype.search = function (query, success, error) { 572 var that = this; 573 574 var urlparams = that.getJsonFromUrl(); 575 576 that.log('trigger:bxmaker.geoip.search.before', { 577 'query': query, 578 'success': success, 579 'error': error 580 }); 581 $(document).trigger('bxmaker.geoip.search.before', { 582 'query': query, 583 'success': success, 584 'error': error 585 }); 586 587 if (that.params.useYandexSearch) { 588 that.log('search in yandex - ' + query); 589 590 591 window.BXmakerGeoIPYandexGeo.getConnection().geocode(query, { 592 'kind': 'locality', 593 'results': 100, 594 'json': true 595 }) 596 .then(function (result) { 597 598 that.log('Yandex geocoder success', result); 599 600 var collection = result.GeoObjectCollection; 601 var items = []; 602 603 var responseItems = result.GeoObjectCollection.featureMember; 604 var responseItem = false;
605 var responseItemAddress = []; 606 var item = {}; 607 var bSkip = false; 608 var arLngLat = [0, 0]; 609 610 for (var i in responseItems) { 611 responseItem = responseItems[i]; 612 responseItemAddress = responseItem.GeoObject.metaDataProperty.GeocoderMetaData.Address.Components; 613 614 if (responseItem.GeoObject.metaDataProperty.GeocoderMetaData.kind != 'locality' && responseItem.GeoObject.metaDataProperty.GeocoderMetaData.kind != 'province') continue; 615 616 item = { 617 'location': responseItem.GeoObject.metaDataProperty.GeocoderMetaData.id, 618 'city': responseItem.GeoObject.name, 619 'city_id': responseItem.GeoObject.metaDataProperty.GeocoderMetaData.id, 620 'country': '', 621 'country_id': 0, 622 'region': '', 623 'region_id': 0, 624 'area': '', 625 'zip': '000000', 626 'range': 0, 627 'lng': 0, 628 'lat': 0, 629 'yandex': 1 630 }; 631
632 bSkip = false; 633 for (var ci = 0; ci < that.params.yandexSearchSkipWords.length; ci++) { 634 if (item['city'].indexOf(that.params.yandexSearchSkipWords[ci].replace(/^\s+/, '').replace(/\s+$/)) != -1) { 635 that.log('Skip location ' + item['city'] + ': skip word = ' + that.params.yandexSearchSkipWords[ci]); 636 bSkip = true; 637 } 638 } 639 640 if (bSkip) continue; 641 642 arLngLat = responseItem.GeoObject.Point.pos.split(' '); 643 item['lng'] = arLngLat[0]; 644 item['lat'] = arLngLat[1]; 645 646 for (var ai in responseItemAddress) { 647 switch (responseItemAddress[ai].kind) { 648 case 'locality': { 649 item['city'] = responseItemAddress[ai].name; 650 break; 651 } 652 case 'area': { 653 item['area'] = responseItemAddress[ai].name; 654 break; 655 } 656 case 'province': { 657 item['region'] = responseItemAddress[ai].name; 658 break; 659 } 660 case 'country': { 661 item['country'] = responseItemAddress[ai].name; 662 break; 663 } 664 } 665 } 666 667 items.push(item); 668 } 669 670 items.splice(10); 671 672 success({ 673 response: { 674 items: items, 675 count: items.length 676 } 677 }); 678 679 that.log('trigger:bxmaker.geoip.search.after', { 680 response: { 681 items: items, 682 count: items.length 683 } 684 }); 685 $(document).trigger('bxmaker.geoip.search.after', { 686 response: { 687 items: items, 688 count: items.length 689 } 690 }); 691 692 }, function (r) { 693 that.logError('Yandex geocoder error', r); 694 695 error(r); 696 that.log('trigger:bxmaker.geoip.search.after', { 697 'error': { 698 'code': 'error_ajax', 699 'msg': 'error response', 700 'more': r 701 } 702 }); 703 $(document).trigger('bxmaker.geoip.search.after', { 704 'error': { 705 'code': 'error_ajax', 706 'msg': 'error response', 707 'more': r 708 } 709 }); 710 }); 711 } 712 else { 713 that.log('search in bitrix - ' + query); 714 715 $.ajax({ 716 type: 'POST', 717 url: that.params.ajaxUrl + (!!urlparams && !!urlparams.clear_cache ? '?clear_cache=' + urlparams.clear_cache : ''), 718 dataType: 'json', 719 data: { 720 method: 'search', 721 sessid: BX.bitrix_sessid(), 722 parameters: that.params.parameters, 723 template: that.params.template, 724 siteId: that.params.siteId, 725 query: query 726 }, 727 error: function (r) { 728 if (typeof error == 'function') { 729 that.logError('Error connection', r); 730 731 error(r); 732 } 733 that.log('trigger:bxmaker.geoip.search.after', { 734 'error': { 735 'code': 'error_ajax', 736 'msg': 'error response', 737 'more': r 738 } 739 }); 740 $(document).trigger('bxmaker.geoip.search.after', { 741 'error': { 742 'code': 'error_ajax', 743 'msg': 'error response', 744 'more': r 745 } 746 }); 747 }, 748 success: function (r) { 749 if (!!r.response) { 750 if (typeof success == 'function') { 751 that.log('Search success', r); 752 753 success(r); 754 } 755 } 756 else if (!!r.error) { 757 if (typeof error == 'function') { 758 that.logError('Error', r); 759 760 error(r); 761 } 762 } 763 that.log('trigger:bxmaker.geoip.search.after', r); 764 $(document).trigger('bxmaker.geoip.search.after', r); 765 } 766 }); 767 } 768 return true; 769 }; 770 771 /** 772 * Óñòàíîâêà â êà÷åñòâå òåêóùåãî íîâîãî ìåñòîïîëîæåíèÿ 773 * @param id - èäåíòèôèêàòîð íîâîãî ìåñòîïîëîæåíèÿ 774 * @param params 775 * 776 * @event "bxmaker.geoip.select.location.before" 777 * @event "bxmaker.geoip.select.location.after" 778 * @returns {boolean} 779 */ 780 BXmakerGeoIPCityConstructor.prototype.selectLocation = function (id, params) { 781 var that = this; 782 var params = params || {}; 783 var currentLocation = that.getLocation(); 784 var currentCity = that.getCity(); 785 var urlparams = that.getJsonFromUrl(); 786 787 that.log('trigger:bxmaker.geoip.select.location.before', { 788 id: id, 789 params: params 790 }); 791 $(document).trigger('bxmaker.geoip.select.location.before', { 792 id: id, 793 params: params 794 }); 795 796 that.log('select location ', id, params); 797 798 params['sessid'] = BX.bitrix_sessid(); 799 params['parameters'] = that.params.parameters; 800 params['template'] = that.params.template; 801 params['siteId'] = that.params.siteId; 802 803 if (id) { 804 params['method'] = 'selectLocation'; 805 params['id'] = id; 806 } 807 else { 808 params['method'] = 'selectYandexLocation'; 809 } 810 811 $.ajax({ 812 type: 'POST', 813 url: that.params.ajaxUrl + (!!urlparams && !!urlparams.clear_cache ? '?clear_cache=' + urlparams.clear_cache : ''), 814 dataType: 'json', 815 data: params, 816 error: function (r) { 817 that.logError('error ajax select location', r); 818 819 that.log('trigger: bxmaker.geoip.select.location.after', { 820 'error': { 821 'code': 'error_ajax', 822 'msg': 'error response', 823 'more': r 824 } 825 }); 826 $(document).trigger('bxmaker.geoip.select.location.after', { 827 'error': { 828 'code': 'error_ajax', 829 'msg': 'error response', 830 'more': r 831 } 832 }); 833 }, 834 success: function (r) { 835 if (!!r.response) { 836 837 that.log('success select location', r); 838 839 if (!!r.response.redirect_src) { 840 var image = new Image(); 841 image.onerror = function(){ 842 // location.href = r.response.redirect; 843 location.replace(r.response.redirect); 844 }; 845 image.src = r.response.redirect_src; 846 return true; 847 } 848 else if (!!r.response.redirect) { 849 // location.href = r.response.redirect; 850 location.replace(r.response.redirect); 851 return true; 852 } 853 854 855 if (that.params.reload) { 856 that.log('need page reload'); 857 858 //åñëè åñòü ôîðìà îôîðìëåíèÿ çàêàçà 859 if (!!BX.Sale && !!BX.Sale.OrderAjaxComponent && !!BX.Sale.OrderAjaxComponent.sendRequest) { 860 that.log('order page - send request'); 861 // BX.Sale.OrderAjaxComponent.sendRequest() 862 } 863 else if (!!BX.saleOrderAjax) { 864 that.log('order page old - unknown action') 865 //window.location.reload(); 866 } 867 else { 868 that.log('reload'); 869 window.location.reload(); 870 return; 871 } 872 } 873 874 if (!!r.response && !!r.response.city && !!r.response.location && (r.response.city != currentCity || r.response.location != currentLocation)) { 875 var params = that.getFullData(); 876 params['response'] = r.response; 877 878 that.log('trigger: bxmaker.geoip.city.change', params); 879 $(document).trigger('bxmaker.geoip.city.change', params); 880 } 881 882 that.showCity(); 883 884 that.log('trigger: bxmaker.geoip.select.location.after', { 885 'response': r.response 886 }); 887 $(document).trigger('bxmaker.geoip.select.location.after', { 888 'response': r.response 889 }); 890 891 892 } 893 else if (!!r.error) { 894 895 that.logError('error select location ', r); 896 897 that.log('trigger: bxmaker.geoip.select.location.after', { 898 'error': r.error 899 }); 900 901 $(document).trigger('bxmaker.geoip.select.location.after', { 902 'error': r.error 903 }); 904 } 905 } 906 }); 907 908 return true; 909 }; 910 911 /** 912 * Ðàññòàâëÿåò íàçâàíèå ãîðîäà â ðàçìå÷åííûå ìåñòà, âûçûâàåò îñáûòèå 913 * @event "bxmaker.geoip.city.show" 914 */ 915 BXmakerGeoIPCityConstructor.prototype.showCity = function () { 916 var that = this; 917 918 var params = that.getFullData(); 919 920 that.log('show city', params); 921 922 $('.js-bxmaker__geoip__city-name-global').text(params['city']); 923 924 //ñîáûòèå ïîêàçà òåêóùåãî ãîðîäà, ïîñëå ïðîñòàíîâêè â ðàçìå÷åííûõ ìåñòàõ 925 that.log('trigger: bxmaker.geoip.city.show', params); 926 $(document).trigger('bxmaker.geoip.city.show', params); 927 }; 928 929 /** 930 * Âîçâðàøùàåò ìàññèâ ñ îïèñàíèå ìåñòîïîëîæåíèÿ ïîëüçîâàòåëÿ
931 * @returns {mixed} 932 */ 933 BXmakerGeoIPCityConstructor.prototype.getFullData = function () { 934 var that = this; 935 936 return { 937 'location': that.getLocation(), 938 'location_code': that.getLocationCode(), 939 'city': that.getCity(), 940 'city_id': that.getLocation(), 941 'country': that.getCountry(), 942 'country_id': that.getCountryId(), 943 'region': that.getRegion(), 944 'region_id': that.getRegionId(), 945 'area': that.getArea(), 946 'zip': that.getZip(), 947 'range': that.getRange(), 948 'lng': that.getLng(), 949 'lat': that.getLat(), 950 'yandex': that.getYandex() 951 }; 952 }; 953 954 955 /** 956 * Âîçâðàùàåò èäåíòèôèêàòîð ìåñòîïîëîæåíèÿ 957 * @returns {number} 958 */ 959 BXmakerGeoIPCityConstructor.prototype.getLocation = function () { 960 var that = this; 961 return that.cookie('location') || 0; 962 }; 963 964 /** 965 * Âîçâðàùàåò êîä ìåñòîïîëîæåíèÿ, íàïðèìåð 000001256 966 * @returns {string} 967 */ 968 BXmakerGeoIPCityConstructor.prototype.getLocationCode = function () { 969 var that = this; 970 return that.cookie('location_code') || ''; 971 }; 972 973 BXmakerGeoIPCityConstructor.prototype.getCity = function () { 974 var that = this; 975 return that.cookie('city') || ''; 976 }; 977 978 BXmakerGeoIPCityConstructor.prototype.getCityId = function () { 979 var that = this; 980 return that.cookie('city_id') || 0; 981 }; 982 983 BXmakerGeoIPCityConstructor.prototype.getCountry = function () { 984 var that = this; 985 return that.cookie('country') || false; 986 }; 987 988 BXmakerGeoIPCityConstructor.prototype.getCountryId = function () { 989 var that = this; 990 return that.cookie('country_id') || 0; 991 }; 992 993 BXmakerGeoIPCityConstructor.prototype.getRegion = function () { 994 var that = this; 995 return that.cookie('region') || false; 996 }; 997 998 BXmakerGeoIPCityConstructor.prototype.getRegionId = function () { 999 var that = this; 1000 return that.cookie('region_id') || 0; 1001 }; 1002 1003 BXmakerGeoIPCityConstructor.prototype.getArea = function () { 1004 var that = this; 1005 return that.cookie('area') || false; 1006 }; 1007 1008 /** 1009 * Âîçâðàùàåò èíäåêñ ìåñòîïîëîæåíèÿ åñëè îí åñòü 1010 * @returns {string} 1011 */ 1012 BXmakerGeoIPCityConstructor.prototype.getZip = function () { 1013 var that = this; 1014 return that.cookie('zip') || ''; 1015 }; 1016 1017 BXmakerGeoIPCityConstructor.prototype.getLng = function () { 1018 var that = this; 1019 return that.cookie('lng') || 0; 1020 }; 1021 1022 BXmakerGeoIPCityConstructor.prototype.getLat = function () { 1023 var that = this; 1024 return that.cookie('lat') || 0; 1025 }; 1026 1027 BXmakerGeoIPCityConstructor.prototype.getRange = function () { 1028 var that = this; 1029 return that.cookie('range') || false; 1030 }; 1031 1032 /** 1033 * Âîçâðàùàåò ôëàã âêëþ÷åí ëè ïîèñê ãîðîäà ÷åðåç ÿíäåêñ 1034 * @returns {boolean} 1035 */ 1036 BXmakerGeoIPCityConstructor.prototype.getYandex = function () { 1037 var that = this; 1038 return !!(that.cookie('yandex') || that.params.useYandexSearch); 1039 }; 1040 1041 /** 1042 * Ïðîâåðêà íåîáõîäèìîñòè â ðåäèðåêòå è ïåðåàäðåñàöèÿ â ñëó÷àå íåîáõîäèìîñòè 1043 */ 1044 BXmakerGeoIPCityConstructor.prototype.checkRedirect = function () { 1045 var that = this; 1046 var urlparams = that.getJsonFromUrl(); 1047 1048 // åñëè âêëþ÷åíû ðåäèðåêòû ìåæäó äîìåíàìè 1049 if (that.params.useDomainRedirect) { 1050 1051 // åñëè íå ïðîâîäèëàñü ïðïîâåðêà íåîáõîäèìîñòè ðåäèðåêòà 1052 if (that.cookie('checked_redirect') != 'Y') { 1053 1054 $.ajax({ 1055 type: 'POST', 1056 url: that.params.ajaxUrl + (!!urlparams && !!urlparams.clear_cache ? '?clear_cache=' + urlparams.clear_cache : ''), 1057 dataType: 'json', 1058 data: { 1059 method: 'checkNeedRedirect', 1060 sessid: BX.bitrix_sessid(), 1061 parameters: that.params.parameters, 1062 template: that.params.template, 1063 siteId: that.params.siteId 1064 }, 1065 error: function (r) { 1066 that.logError('error ajax checked_redirect', r); 1067 }, 1068 success: function (r) { 1069 if (!!r.response) { 1070 1071 that.log('success checked_redirect', r); 1072 1073 if (!!r.response.need) { 1074 that.cookie('checked_redirect', 'Y'); 1075 1076 // ðàñïðîñòðàíåíèå êóê íà äðóãèå äîìåíû + ðåäèðåêò 1077 if (!!r.response.redirect_src) { 1078 var image = new Image(); 1079 image.onerror = function(){ 1080 location.replace(r.response.redirect); 1081 }; 1082 image.src = r.response.redirect_src; 1083 } 1084 // ðåäèðåêò áåç ðàñïðîñòðàíåíèÿ 1085 else if (!!r.response.redirect) { 1086 location.replace(r.response.redirect); 1087 } 1088 1089 } 1090 } 1091 } 1092 }); 1093 } 1094 // åñëè ïðîâåðêà ðåäèðåêòà ïðîèñõîäèëà 1095 else { 1096 //ïðîâåðêà, îòëè÷àþòñÿ ëè äîìåíû, òåêóùèé è îæèäàåìûé - ïåðåàäðåñîâûâàåì ñ ñîõðàíåíèåì ïóòè è ïàðàìåòðîâ ñòðàíèöû 1097 if (that.params.locationDomain != location.hostname) { 1098 var url = location.protocol; 1099 url += '//'; 1100 url += that.params.locationDomain; 1101 url += location.pathname; 1102 url += location.search; 1103 url += location.hash; 1104 1105 location.href = url; 1106 } 1107 } 1108 } 1109 }; 1110 1111 1112 /** 1113 * Ïðîâåðêà âîçìîæíîñòè èñîïëüçîâàòü ëîêàëüíûå õðàíèëèùà 1114 * @returns {boolean} 1115 */ 1116 BXmakerGeoIPCityConstructor.prototype.storageTest = function () { 1117 try { 1118 return 'localStorage' in window && window['localStorage'] !== null; 1119 } catch (e) { 1120 return false;
1121 } 1122 }; 1123 1124 /** 1125 * Ñîõðàíÿåò äàííûå â ëîêàëüíîì õðàíèëèùå 1126 * @param key 1127 * @param value 1128 * @returns {*} 1129 */ 1130 BXmakerGeoIPCityConstructor.prototype.storageSet = function (key, value) { 1131 var that = this; 1132 key = that.params.cookiePrefix + key; 1133 try { 1134 if (value === null) { 1135 return window.localStorage.removeItem(key); 1136 } 1137 else { 1138 return window.localStorage.setItem(key, JSON.stringify({ 1139 "data": value, 1140 "time": (new Date()).getTime() 1141 })); 1142 } 1143 } catch (e) { 1144 if (e == QUOTA_EXCEEDED_ERR) { 1145 that.logError('LocalStorage limit exceeded'); 1146 } 1147 else { 1148 that.logError('localStorage:', e); 1149 } 1150 return false; 1151 } 1152 }; 1153 1154 /** 1155 * Âîçâðàùàåò äàííûå èç ëîêàëüíîãî õðàíèëèùà 1156 * @param key 1157 * @returns {*} 1158 */ 1159 BXmakerGeoIPCityConstructor.prototype.storageGet = function (key) { 1160 var that = this; 1161 var time = (new Date()).getTime(); 1162 key = that.params.cookiePrefix + key; 1163 try { 1164 var obj = JSON.parse(window.localStorage.getItem(key)); 1165 if (!!obj) { 1166 if (!!obj['time'] && obj['time'] > (time - 60 * 60 * 24 * 3)) { 1167 return obj['data']; 1168 } 1169 } 1170 return false; 1171 } catch (e) { 1172 that.logError('localStorage:', e); 1173 return false; 1174 } 1175 }; 1176 1177 window.BXmakerGeoIPCity = new BXmakerGeoIPCityConstructor(); 1178 1179 })(); 1180} 1181 1182 1183if (!!window.BXmakerGeoIPYandexGeo === false) { 1184 1185 (function () { 1186 1187 var GeoIPYandexGeo = function () { 1188 var that = this; 1189 1190 that.bInit = false; 1191 that.ymap = false; 1192 that.debug = false; 1193 that.cb = []; 1194 }; 1195 1196 GeoIPYandexGeo.prototype.log = function () { 1197 if (this.debug) { 1198 var args = Array.prototype.slice.call(arguments); 1199 args.unshift('bxmaker:geoip.city.yandex.geo: '); 1200 console.log.apply(console, args); 1201 } 1202 }; 1203 GeoIPYandexGeo.prototype.logError = function () { 1204 if (this.debug) { 1205 var args = Array.prototype.slice.call(arguments); 1206 args.unshift('bxmaker:geoip.city.yandex.geo: '); 1207 console.error.apply(console, args); 1208 } 1209 }; 1210 1211 1212 GeoIPYandexGeo.prototype.init = function (params) { 1213 var that = this; 1214 1215 if (that.bInit) return true; 1216 1217 // ïðîâåðêà âêëþ÷åíà ëè îòëàäêà âðó÷íóþ 1218 if (!!window.BXmakerGeoIPCity && window.BXmakerGeoIPCity.cookie('debug') == 'Y') { 1219 that.debug = true; 1220 } 1221 1222 that.bInit = true; //&ns=BXmakerGeoIPYandexMap 1223 $('head').append($('<script id="BXmakerGeoIPYandexGeo" type="text/javascript" src="https://api-maps.yandex.ru/2.1/?lang=ru_RU&load=geolocation,geocode&ns=&onload=BXmakerGeoIPYandexGeo.onload" />')); 1224 }; 1225 1226 GeoIPYandexGeo.prototype.onload = function (ym) { 1227 var that = this; 1228 that.ymap = ym; 1229 1230 that.log('API is loaded'); 1231 1232 that.log('trigger: bxmaker.geoip.yandex.geo.loaded'); 1233 $(document).trigger('bxmaker.geoip.yandex.geo.loaded'); 1234 }; 1235 1236 GeoIPYandexGeo.prototype.getConnection = function () { 1237 var that = this; 1238 return that.ymap; 1239 }; 1240 1241 1242 window.BXmakerGeoIPYandexGeo = new GeoIPYandexGeo(); 1243 })(); 1244 1245}
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.