PageSourceSearch

https://beeline-ru.online/wp-content/themes/beeline/js/popup-select-city.js

js beeline-ru.online collected 2026-10-08 07:17:56 UTC 6,884 bytes, 140 lines download raw bytes

1document.addEventListener('DOMContentLoaded', function() {
2  const popupSelectCity = document.getElementById('popup-select-city');
3  const closeButton = popupSelectCity.querySelector('.popup__close');
4  let openedFromPopupTopForm = false;
5
6  // LANDINGI-796: 7 дней хранения выбора города, сделанного внутри бизнес-попапа
7  const BUSINESS_CITY_COOKIE = 'business_popup_city';
8  const BUSINESS_CITY_COOKIE_DAYS = 7;
9
10  function saveBusinessCityCookie(id, name) {
11    const value = encodeURIComponent(JSON.stringify({ id: id, name: name }));
12    const expires = new Date(Date.now() + BUSINESS_CITY_COOKIE_DAYS * 24 * 60 * 60 * 1000).toUTCString();
13    document.cookie = BUSINESS_CITY_COOKIE + '=' + value + '; expires=' + expires + '; path=/';
14  }
15
16  function showPopup(event) {
17    // Проверяем, был ли клик на любом элементе .city
18    const clickedCityElement = event.target.closest('.city');
19    if (clickedCityElement) {
20      openedFromPopupTopForm = true; // Клик был по .city
21    } else {
22      openedFromPopupTopForm = false; // Клик не был по .city
23    }
24
25    // LANDINGI-796: помечаем, что попап открыт из бизнес-попапа — дальше это
26    // читают и здесь (ветка ниже), и в cf7-address-search.js (handleGlobalCityChange),
27    // чтобы не задваивать работу и не трогать другие формы страницы.
28    popupSelectCity.dataset.openedFrom = event.target.closest('.popup-business') ? 'business' : '';
29
30    popupSelectCity.style.display = 'flex';
31  }
32
33  document.querySelectorAll('.button-select-city').forEach(button => {
34    button.addEventListener('click', showPopup);
35  });
36
37  document.querySelectorAll('.city').forEach(city => {
38    city.addEventListener('click', showPopup);
39  });
40
41  popupSelectCity.addEventListener('click', function(e) {
42    if (e.target.classList.contains('region_item')) {
43      const popup_cityId = e.target.getAttribute('id');
44      const cityName = e.target.textContent.trim();
45
46      // LANDINGI-842: после выбора города подтверждение региона больше не показываем.
47      // Пока окно открыто, плашка лежит под его подложкой (z-index 1001 > 1000).
48      const popupConfirmRegion = document.getElementById('popup-confirm-region');
49      if (popupConfirmRegion) {
50        popupConfirmRegion.style.display = 'none';
51      }
52
53      if (popupSelectCity.dataset.openedFrom === 'business') {
54        // LANDINGI-796: выбор города из бизнес-попапа. Пишем только в форму
55        // внутри .popup-business — другие формы страницы не трогаем.
56        e.preventDefault();
57
58        const businessPopup = document.querySelector('.popup-business');
59        const businessForm = businessPopup ? businessPopup.querySelector('form.wpcf7-form') : null;
60        if (businessForm) {
61          const cityNameDisplay = businessForm.querySelector('.autocomplete-city-name');
62          const hiddenCityId = businessForm.querySelector('.id_city');     // BusinessCityId
63          const hiddenCityName = businessForm.querySelector('.name_city'); // CityName
64          const addressInput = businessForm.querySelector('[name="City"]');
65
66          if (cityNameDisplay) {
67            cityNameDisplay.textContent = cityName;
68            cityNameDisplay.setAttribute('data-item', popup_cityId);
69          }
70          if (hiddenCityId) hiddenCityId.value = popup_cityId;
71          if (hiddenCityName) hiddenCityName.value = cityName;
72          if (addressInput) addressInput.disabled = false;
73
74          saveBusinessCityCookie(popup_cityId, cityName);
75
76          // LANDINGI-796: ручной выбор держится до перезагрузки/перехода на другую
77          // страницу, даже на странице, привязанной к городу — иначе следующее
78          // открытие попапа снова подставит город страницы поверх выбора
79          // пользователя. dataset живёт в DOM, значит переживает закрытие попапа,
80          // но не переживает перезагрузку — этого и требует ТЗ.
81          if (businessPopup) {
82            businessPopup.dataset.manualCityId = popup_cityId;
83            businessPopup.dataset.manualCityName = cityName;
84          }
85        }
86      } else if (!openedFromPopupTopForm) {
87        e.preventDefault();
88        // Если не из popupTopForm — вставляем значения
89        const cityNameElements = document.querySelectorAll('.autocomplete-city-name');
90        cityNameElements.forEach(function(cityNameElement) {
91          cityNameElement.textContent = cityName;
92          cityNameElement.setAttribute('data-item', popup_cityId);
93		});
94		  // 		// 	Очистка полей с адресом и домом при смене города
95		  // 		const addressStreet = document.getElementById('address_street');
96		  // 		  if (addressStreet && addressStreet.value.trim() !== '') {
97		  // 			addressStreet.value = '';
98		  // 		  }
99
100		  // 		  const addressHouse = document.getElementById('address_house');
101		  // 		  if (addressHouse && addressHouse.value.trim() !== '') {
102		  // 			addressHouse.value = '';
103		  // 			addressHouse.removeAttribute('data-street-id'); // если надо убрать привязку к улице
104		  // 		  }
105
106		  // Очистка полей с адресом и домом при смене города
107		  document.querySelectorAll('[name="AddresStreet"]').forEach(streetInput => {
108			  streetInput.value = '';
109		  });
110
111		  document.querySelectorAll('[name="AddresHouse"]').forEach(houseInput => {
112			  houseInput.value = '';
113			  houseInput.removeAttribute('data-street-id'); // убираем привязку к улице
114			  houseInput.disabled = true;                   // блокируем снова
115			  houseInput.placeholder = 'Дом';
116			  
117			  // Очистка блока с адресом
118			  const addressDiv = document.getElementById('address-block__title');
119			  if (addressDiv) {
120				  addressDiv.textContent = '';
121			  }
122		  });
123		  
124	  } else {
125		  // Если из popupTopForm — не вставляем
126		  console.log('Выбор города из popupTopForm — не вставляем значения');
127	  }
128
129		popupSelectCity.style.display = 'none';
130	}
131
132    if (e.target === popupSelectCity) {
133      popupSelectCity.style.display = 'none';
134    }
135  });
136
137  closeButton.addEventListener('click', function() {
138    popupSelectCity.style.display = 'none';
139  });
140});

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.