· 5 years ago · Jul 14, 2020, 06:38 PM
1$(function () {
2
3 //global vars
4 var ajaxTimeOutId;
5
6 //jquery vars
7 $resContainer = $('.geo-location-window-search-values');
8
9 if ('getPositionIncludeApi' in window && getPositionIncludeApi === true) {
10
11 // fields
12 // jsonObject.isHighAccuracy,
13 // jsonObject.latitude,
14 // jsonObject.longitude,
15 // jsonObject.country,
16 // jsonObject.region,
17 // jsonObject.city,
18 // jsonObject.zoom
19
20 function yandex_init () {
21
22 var geolocation = ymaps.geolocation;
23
24 if (geolocation.city) {
25 $.getJSON(geoPositionAjaxDir + '/ajax.php', {
26 act: 'userPosition',
27 latitude: geolocation.latitude,
28 longitude: geolocation.longitude,
29 city: geolocation.city,
30 country: geolocation.country,
31 isHighAccuracy: geolocation.isHighAccuracy,
32 region: geolocation.region,
33 zoom: geolocation.zoom
34 }, function (jsonObject) {
35 if (jsonObject['ERROR'] != 'Y') {
36 //set values
37 $('.geo-location-window-list-item-link[data-id="' + jsonObject.locationID + '"]').trigger('click');
38 $('.user-geo-position-value-link, .geo-location-window-city-value, .geo-location-ref-window-city-value').html(jsonObject.city);
39 $('.geo-location-window-search-input').val(jsonObject.city).data('id', jsonObject.locationID);
40 $('.geo-location-window-button').removeClass('disabled');
41 showLocationRefWindow();
42 } else {
43 showLocationWindow();
44 }
45 });
46 } else {
47 showLocationWindow();
48 }
49
50 }
51
52 function sypex_init () {
53 var sypexLink = typeof geoPositionKey != 'undefined' && geoPositionKey != '' ? '//api.sypexgeo.net/' + geoPositionKey + '/json/' : '//api.sypexgeo.net/';
54 $.getJSON(sypexLink, function (json) {
55 if (typeof (json['city']['name_ru']) != 'undefined') {
56 $.getJSON(geoPositionAjaxDir + '/ajax.php', {
57 act: 'userPosition',
58 latitude: json['city']['lat'],
59 longitude: json['city']['lon'],
60 city: json['city']['name_ru'],
61 country: json['country']['name_ru'],
62 isHighAccuracy: false,
63 region: json['region']['name_ru'],
64 zoom: false
65 }, function (jsonObject) {
66 if (jsonObject['ERROR'] != 'Y') {
67 //set values
68 $('.geo-location-window-list-item-link[data-id="' + jsonObject.locationID + '"]').trigger('click');
69 $('.user-geo-position-value-link, .geo-location-window-city-value, .geo-location-ref-window-city-value').html(jsonObject.city);
70 $('.geo-location-window-search-input').val(jsonObject.city).data('id', jsonObject.locationID);
71 $('.geo-location-window-button').removeClass('disabled');
72 showLocationRefWindow();
73 } else {
74 showLocationWindow();
75 }
76 });
77 } else {
78 showLocationWindow();
79 }
80 });
81 }
82
83 function apiLibPull () {
84 if (typeof apiLibPull['funcUsed'] === 'undefined') {
85 apiLibPull['funcUsed'] = true;
86 } else {
87 return;
88 }
89
90 if (typeof window['skipByCompositeLanguage'] !== 'undefined' && skipByCompositeLanguage()) {
91 return;
92 }
93
94 if (geoPositionEngine == 'YANDEX') {
95
96 //load yandex map script
97 var yandexMapLoader = document.createElement('script');
98 yandexMapLoader.src = '//api-maps.yandex.ru/2.0/?load=package.standard&lang=ru-RU';
99
100 //check api key
101 if (typeof geoPositionYandexKey != 'undefined' && geoPositionYandexKey != '') {
102 yandexMapLoader.src += '&apikey=' + geoPositionYandexKey;
103 }
104
105 yandexMapLoader.className = 'yaMapLoaderScript';
106
107 document.body.appendChild(yandexMapLoader);
108 yandexMapLoader.onload = function () {
109 if (typeof ymaps == 'object' && typeof ymaps.ready == 'function') {
110 ymaps.ready(yandex_init);
111 }
112 };
113
114 //check 1 sec for load ya script
115 setTimeout(function () {
116 if (typeof ymaps != 'object') {
117 $('.yaMapLoaderScript').remove();
118 }
119 }, 1000);
120
121 } else {
122 sypex_init();
123 }
124 }
125
126 window.apiLibPull = apiLibPull;
127
128 function checkAndLazyInit () {
129 let indicatorOnLazy = document.querySelector('.user-geo-position[data-skip-onload]');
130 if (indicatorOnLazy) {
131 return;
132 }
133 apiLibPull();
134 }
135
136 checkAndLazyInit();
137 }
138
139 var getSearchCity = function ($input, query) {
140
141 //loader
142 $input.addClass('loading');
143
144 //clear container
145 $resContainer.empty();
146
147 //get location list
148 $.getJSON(geoPositionAjaxDir + '/ajax.php?act=locSearch&query=' + encodeURI(query), function (jsonData) {
149 $input.removeClass('loading');
150 if (jsonData['ERROR'] != 'Y') {
151 $.each(jsonData, function (i, arValues) {
152 $resContainer.append(
153 $('<div />', { class: 'geo-location-list-item' }).append(
154 $('<a />', { class: 'geo-location-list-item-link' }).html(arValues['COUNTRY_NAME'] + ', ' + arValues['CITY_NAME']).attr('href', '#').data('id', arValues['ID']).data('parse-value', arValues['CITY_NAME'])
155 )
156 );
157 });
158 }
159 });
160
161 };
162
163 var pressSearchField = function (event) {
164
165 var $this = $(this);
166 var thisValue = $this.val();
167
168 if (thisValue.length > 1 && !clearTimeout(ajaxTimeOutId)) {
169 ajaxTimeOutId = setTimeout(
170 function () {
171 getSearchCity($this, thisValue);
172 }, 350
173 );
174 }
175
176 };
177
178 var selectLocationFromFastView = function (event) {
179
180 var $this = $(this);
181 var thisID = $this.data('id');
182 var thisValue = $this.data('parse-value');
183
184 $('.geo-location-window-search-input').val(thisValue).data('id', thisID);
185 $('.geo-location-window-city-value').html(thisValue);
186
187 var $locationWindowList = $('.geo-location-window-list');
188 var $locationWindowListLinks = $locationWindowList.find('.geo-location-window-list-item-link').removeClass('selected');
189
190 $locationWindowListLinks.each(function (index, el) {
191 var $nextElement = $(el);
192 if ($nextElement.data('id') == thisID) {
193 $nextElement.addClass('selected');
194 return false;
195 }
196 });
197
198 $('.geo-location-window-button').removeClass('disabled').addClass('modifed');
199
200 $resContainer.empty();
201
202 return event.preventDefault();
203
204 };
205
206 var setLocationFromServer = function (event) {
207
208 var $this = $(this).addClass('loading');
209
210 $.getJSON(geoPositionAjaxDir + '/ajax.php', {
211 act: 'setLocation',
212 locationID: $('.geo-location-window-search-input').data('id')
213 }, function (jsonData) {
214 if (jsonData['SUCCESS'] == 'Y') {
215 window.location.reload();
216 }
217 });
218
219 return event.preventDefault();
220
221 };
222
223 var showLocationWindow = function () {
224 if (getCookie('locationWindowClose') != 'Y') {
225 $('#geo-location-window').removeClass('hidden').appendTo('body');
226 }
227 };
228
229 var openLocationWindow = function (event) {
230 $('#geo-location-window').removeClass('hidden').appendTo('body').show();
231 return event.preventDefault();
232 };
233
234 var closeLocationWindow = function (event) {
235 var currentDate = new Date(new Date().getTime() + 128000 * 1000);
236 document.cookie = 'locationWindowClose=Y; path=/; expires=' + currentDate.toUTCString();
237 $('#geo-location-window').hide();
238 return event.preventDefault();
239 };
240
241 var showLocationRefWindow = function () {
242
243 //jquery vars
244 var $positionLink = $('.user-geo-position .user-geo-position-value-link');
245 var $positionWindow = $('#geo-location-ref-window');
246 var $window = $(window);
247
248 //disabled
249 if ($positionWindow.data('disabled') == 'Y') {
250 return false;
251 }
252
253 //ref window
254 var refwidth = $positionWindow.width();
255 var windowWidth = $window.innerWidth();
256
257 //other
258 var coordinates = {
259 left: ($positionLink.offset().left - refwidth / 2),
260 top: ($positionLink.offset().top + 42)
261 };
262
263 //move
264 $positionWindow.appendTo($('body'));
265
266 //display ref window
267 if (windowWidth <= 1024 || (refwidth + $positionLink.offset().left) > windowWidth) {
268 $positionWindow.addClass('visible centred');
269 }
270
271 //position
272 else {
273 $positionWindow.addClass('visible').css({
274 left: coordinates.left,
275 top: coordinates.top
276 });
277 }
278
279 };
280
281 var closeLocationRefWindow = function () {
282
283 //jquery vars
284 var $this = $(this).addClass('loading');
285
286 //reload page
287 window.location.reload();
288
289 //block actions
290 return event.preventDefault();
291
292 };
293
294 function getCookie (name) {
295
296 //vars
297 var cookie = ' ' + document.cookie;
298 var search = ' ' + name + '=';
299 var setStr = null;
300 var offset = 0;
301 var end = 0;
302
303 if (cookie.length > 0) {
304 offset = cookie.indexOf(search);
305 if (offset != -1) {
306 offset += search.length;
307 end = cookie.indexOf(';', offset);
308 if (end == -1) {
309 end = cookie.length;
310 }
311 setStr = unescape(cookie.substring(offset, end));
312 }
313 }
314
315 return (setStr);
316
317 }
318
319 $(document).on('keyup', '.geo-location-window-search-input', pressSearchField);
320 $(document).on('click', '.geo-location-list-item-link', selectLocationFromFastView);
321 $(document).on('click', '.geo-location-window-list-item-link', selectLocationFromFastView);
322 $(document).on('click', '.geo-location-window-button', setLocationFromServer);
323 $(document).on('click', '.geo-location-window-exit', closeLocationWindow);
324 $(document).on('click', '.get-location-ref-window-confirm-button', closeLocationRefWindow);
325 $(document).on('click', '.user-geo-position-value-link, .get-location-ref-window-change-button', openLocationWindow);
326
327});