1(function () { 2 if (window.chelznakTagSubscriptionInit) { 3 return; 4 } 5 window.chelznakTagSubscriptionInit = true; 6 var storagePrefix = 'chelznak.tagSubscription.'; 7 var emailStorageKey = storagePrefix + 'email'; 8 9 function getStorageKey(widget) { 10 return storagePrefix + widget.getAttribute('data-tag-id'); 11 } 12 13 function saveSubscribed(widget) { 14 try { 15 window.localStorage.setItem(getStorageKey(widget), '1'); 16 } catch (e) {} 17 } 18 19 function removeStoredSubscribed(widget) { 20 try { 21 window.localStorage.removeItem(getStorageKey(widget)); 22 } catch (e) {} 23 } 24 25 function saveSessionSubscribed(widget) { 26 try { 27 window.sessionStorage.setItem(getStorageKey(widget), '1'); 28 } catch (e) {} 29 } 30 31 function isSessionSubscribed(widget) { 32 try { 33 return window.sessionStorage.getItem(getStorageKey(widget)) === '1'; 34 } catch (e) { 35 return false; 36 } 37 } 38 39 function isStoredSubscribed(widget) { 40 try { 41 return window.localStorage.getItem(getStorageKey(widget)) === '1'; 42 } catch (e) { 43 return false; 44 } 45 } 46 47 function saveEmail(email) { 48 var normalizedEmail = email.trim().toLowerCase(); 49 if (!normalizedEmail) { 50 return; 51 } 52 53 try { 54 window.localStorage.setItem(emailStorageKey, normalizedEmail); 55 } catch (e) {} 56 } 57 58 function getSavedEmail() { 59 try { 60 return window.localStorage.getItem(emailStorageKey) || ''; 61 } catch (e) { 62 return ''; 63 } 64 } 65 66 function prefillEmail(modal) { 67 var input = modal ? modal.querySelector('[name="email"]') : null; 68 if (!input || input.value.trim()) { 69 return; 70 } 71 72 var email = getSavedEmail(); 73 if (email) { 74 input.value = email; 75 } 76 } 77 78 function setMessage(widget, message, type) { 79 var messageNode = widget.querySelector('.js-tag-subscribe-message'); 80 if (messageNode) { 81 messageNode.textContent = message; 82 messageNode.className = 'tag-subscription__message js-tag-subscribe-message is-' + type; 83 } 84 } 85 86 function setModalMessage(modal, message, type) { 87 var messageNode = modal ? modal.querySelector('.js-tag-subscribe-modal-message') : null; 88 if (messageNode) { 89 messageNode.textContent = message; 90 messageNode.className = 'tag-subscription-modal__message js-tag-subscribe-modal-message' + (type ? ' is-' + type : ''); 91 } 92 } 93 94 function showMessage(widget, message, type) { 95 setMessage(widget, message, type); 96 if (typeof Informer !== 'undefined') { 97 new Informer({text: message, type: type === 'success' ? 'success' : 'error'}).show(); 98 } 99 } 100 101 function setSubscribed(widget) { 102 var button = widget.querySelector('.js-tag-subscribe-button, .tag-subscription__button'); 103 if (button) { 104 button.style.display = ''; 105 button.textContent = 'ÐÑ Ð¿Ð¾Ð´Ð¿Ð¸ÑанÑ'; 106 button.disabled = true; 107 button.classList.add('is-subscribed'); 108 } 109 } 110 111 function isAuthorizedWidget(widget) { 112 var button = widget.querySelector('.js-tag-subscribe-button, .tag-subscription__button'); 113 return button && button.getAttribute('data-authorized') === '1'; 114 } 115 116 function getWidgetByTagId(tagId) { 117 return document.querySelector('.js-tag-subscription[data-tag-id="' + tagId + '"]'); 118 } 119 120 function showModalSuccess(modal, data) { 121 if (!modal) { 122 return; 123 } 124 125 while (modal.firstChild) { 126 modal.removeChild(modal.firstChild); 127 } 128 129 var success = document.createElement('div'); 130 success.className = 'tag-subscription-modal__success'; 131 132 var closeButton = document.createElement('button'); 133 closeButton.type = 'button'; 134 closeButton.className = 'tag-subscription-modal__close'; 135 closeButton.setAttribute('data-fancybox-close', ''); 136 closeButton.setAttribute('aria-label', 'ÐакÑÑÑÑ'); 137 closeButton.addEventListener('click', function () { 138 if (window.jQuery) { 139 if (jQuery.fancybox) { 140 jQuery.fancybox.close(); 141 } 142 } 143 }); 144 success.appendChild(closeButton); 145 146 var title = document.createElement('div'); 147 title.className = 'modal__headline tag-subscription-modal__success-title'; 148 title.textContent = data && data.already_subscribed ? 'ÐÑ Ñже подпиÑанÑ' : 'ÐапÑÐ¾Ñ ÑÑпеÑно оÑпÑавлен'; 149 success.appendChild(title); 150 151 var text = document.createElement('div'); 152 text.className = 'tag-subscription-modal__success-text'; 153 if (data && data.already_subscribed) { 154 text.appendChild(document.createTextNode('ÐÑ Ñже подпиÑалиÑÑ Ð½Ð° ÑÑÐ¾Ñ Ñег Ñанее. УпÑавлÑÑÑ Ñвоими подпиÑками Ð²Ñ Ð¼Ð¾Ð¶ÐµÑе в ')); 155 156 var link = document.createElement('a');
157 link.href = data.subscriptions_url || '/users/subscriptions/'; 158 link.className = 'tag-subscription-modal__cabinet-link'; 159 link.textContent = 'лиÑном кабинеÑе'; 160 text.appendChild(link); 161 text.appendChild(document.createTextNode('.')); 162 } else { 163 text.appendChild(document.createTextNode('ÐÑи новом поÑÑÑплении Ð¼Ñ Ð¾ÑпÑавим вам Ñведомление на поÑÑÑ. УпÑавлÑÑÑ Ñвоими подпиÑками Ð²Ñ Ð¼Ð¾Ð¶ÐµÑе в ')); 164 165 var newLink = document.createElement('a'); 166 newLink.href = data && data.subscriptions_url ? data.subscriptions_url : '/users/subscriptions/'; 167 newLink.className = 'tag-subscription-modal__cabinet-link'; 168 newLink.textContent = 'лиÑном кабинеÑе'; 169 text.appendChild(newLink); 170 text.appendChild(document.createTextNode('.')); 171 } 172 success.appendChild(text); 173 174 modal.appendChild(success); 175 } 176 177 function openModal(widget) { 178 var modal = widget.querySelector('.js-tag-subscription-modal'); 179 if (!modal) { 180 return; 181 } 182 183 prefillEmail(modal); 184 setModalMessage(modal, '', ''); 185 186 if (window.jQuery) { 187 if (jQuery.fancybox) { 188 jQuery.fancybox.open({ 189 src: '#' + modal.id, 190 type: 'inline', 191 opts: { 192 trapFocus: true, 193 infobar: 0, 194 toolbar: 0, 195 slideShow: 0, 196 fullScreen: 0, 197 thumbs: 0, 198 touch: 0, 199 dblclickContent: false, 200 dblclickSlide: false, 201 dblclickOutside: false, 202 afterShow: function () { 203 var input = modal.querySelector('[name="email"]'); 204 if (input) { 205 input.focus(); 206 } 207 } 208 } 209 }); 210 return; 211 } 212 213 if (jQuery.fn) { 214 if (jQuery.fn.fancybox) { 215 jQuery.fancybox.open('#' + modal.id); 216 } 217 } 218 } 219 } 220 221 function getSupportWidget(support) { 222 if (!support) { 223 return null; 224 } 225 226 var widget = null; 227 Array.prototype.forEach.call(support.children, function (child) { 228 if (widget) { 229 return; 230 } 231 232 if (child.classList.contains('js-tag-subscription')) { 233 widget = child; 234 } 235 }); 236 237 return widget; 238 } 239 240 function placeMobileSubscription() { 241 var support = document.querySelector('.shop__support');
242 var widget = getSupportWidget(support) || document.querySelector('.tag-subscription_in-tabs.js-tag-subscription'); 243 if (!widget) { 244 return; 245 } 246 247 var isMobile = false; 248 if (window.matchMedia) { 249 isMobile = window.matchMedia('(max-width: 767px)').matches; 250 } 251 var tabsList = document.querySelector('.shop-tabs__list'); 252 253 if (isMobile) { 254 if (tabsList) { 255 widget.classList.add('tag-subscription_in-tabs'); 256 tabsList.insertAdjacentElement('afterend', widget); 257 } 258 return; 259 } 260 261 if (support) { 262 if (widget.parentNode !== support) { 263 widget.classList.remove('tag-subscription_in-tabs'); 264 support.appendChild(widget); 265 } 266 } 267 } 268 269 function send(widget, email, modal) { 270 var formData = new FormData(); 271 formData.append('tag_id', widget.getAttribute('data-tag-id')); 272 formData.append('csrf', widget.getAttribute('data-csrf')); 273 if (email) { 274 formData.append('email', email); 275 } 276 277 widget.classList.add('is-wait'); 278 if (modal) { 279 modal.classList.add('is-wait'); 280 setModalMessage(modal, '', ''); 281 } 282 return fetch('/chelznak/ajax/tagsubscribe/', { 283 method: 'POST', 284 cache: 'no-cache', 285 body: formData 286 }) 287 .then(function (response) { 288 return response.json().then(function (data) { 289 if (!response.ok || String(data.status).toLowerCase() !== 'ok') { 290 throw new Error(data.message || 'Ðе ÑдалоÑÑ Ð¾ÑоÑмиÑÑ Ð¿Ð¾Ð´Ð¿Ð¸ÑкÑ'); 291 } 292 return data; 293 }); 294 }) 295 .then(function (data) { 296 var responseData = data.data || {}; 297 if (!isAuthorizedWidget(widget)) { 298 saveEmail(email); 299 } 300 setSubscribed(widget); 301 if (isAuthorizedWidget(widget)) { 302 saveSubscribed(widget); 303 } else { 304 removeStoredSubscribed(widget); 305 saveSessionSubscribed(widget); 306 } 307 if (modal) { 308 showModalSuccess(modal, responseData); 309 setMessage(widget, data.message || 'ÐодпиÑка оÑоÑмлена', 'success'); 310 } else { 311 showMessage(widget, data.message || 'ÐодпиÑка оÑоÑмлена', 'success'); 312 } 313 }) 314 .catch(function (error) { 315 if (modal) { 316 setModalMessage(modal, error.message || 'Ðе ÑдалоÑÑ Ð¾ÑоÑмиÑÑ Ð¿Ð¾Ð´Ð¿Ð¸ÑкÑ', 'error'); 317 } 318 showMessage(widget, error.message || 'Ðе ÑдалоÑÑ Ð¾ÑоÑмиÑÑ Ð¿Ð¾Ð´Ð¿Ð¸ÑкÑ', 'error'); 319 }) 320 .finally(function () { 321 widget.classList.remove('is-wait'); 322 if (modal) { 323 modal.classList.remove('is-wait'); 324 } 325 }); 326 } 327 328 function initTagSubscriptionWidgets() { 329 Array.prototype.forEach.call(document.querySelectorAll('.js-tag-subscription'), function (widget) { 330 var messageNode = widget.querySelector('.js-tag-subscribe-message'); 331 if (!isAuthorizedWidget(widget)) { 332 removeStoredSubscribed(widget); 333 if (isSessionSubscribed(widget)) { 334 setSubscribed(widget); 335 if (!messageNode || !messageNode.textContent.trim()) { 336 setMessage(widget, 'ÐодпиÑка оÑоÑмлена', 'success'); 337 } 338 } 339 return; 340 } 341 342 if (isStoredSubscribed(widget)) { 343 setSubscribed(widget); 344 if (!messageNode || !messageNode.textContent.trim()) { 345 setMessage(widget, 'ÐодпиÑка оÑоÑмлена', 'success'); 346 } 347 } 348 }); 349 350 placeMobileSubscription(); 351 } 352 353 document.addEventListener('click', function (event) { 354 var button = event.target.closest('.js-tag-subscribe-button'); 355 if (!button || button.disabled) { 356 return; 357 } 358 359 event.preventDefault(); 360 361 var widget = button.closest('.js-tag-subscription'); 362 if (!widget) { 363 return; 364 } 365 366 openModal(widget); 367 }); 368 369 document.addEventListener('submit', function (event) { 370 var form = event.target.closest('.js-tag-subscribe-form'); 371 if (!form) { 372 return; 373 } 374 375 event.preventDefault(); 376 var modal = form.closest('.js-tag-subscription-modal'); 377 var tagId = modal ? modal.getAttribute('data-tag-id') : ''; 378 var widget = modal ? modal.closest('.js-tag-subscription') : null; 379 if (!widget) { 380 if (tagId) { 381 widget = getWidgetByTagId(tagId); 382 } 383 } 384 var input = form.querySelector('[name="email"]'); 385 var email = input ? input.value.trim() : ''; 386 if (!widget) { 387 return; 388 } 389 390 if (!email) { 391 setModalMessage(modal, 'УкажиÑе email', 'error'); 392 if (input) { 393 input.focus(); 394 } 395 return; 396 } 397 398 send(widget, email, modal); 399 }); 400 401 if (document.readyState === 'loading') { 402 document.addEventListener('DOMContentLoaded', initTagSubscriptionWidgets); 403 } else { 404 initTagSubscriptionWidgets(); 405 } 406 window.addEventListener('resize', placeMobileSubscription); 407})();
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.