@ -4,7 +4,7 @@ document.addEventListener("DOMContentLoaded", (event) => {
Fancybox . bind ( "[data-fancybox]" ) ;
Fancybox . bind ( "[data-fancybox]" ) ;
document . body . addEventListener ( 'submit' , function ( e ) {
document . body . addEventListener ( 'submit' , function ( e ) {
const form = e . target ;
const form = e . target ;
if ( form . matches ( '.add-to-cart-form' ) ) {
if ( form . matches ( '.add-to-cart-form' ) ) {
e . preventDefault ( ) ;
e . preventDefault ( ) ;
@ -14,7 +14,7 @@ document.addEventListener("DOMContentLoaded", (event) => {
}
}
} ) ;
} ) ;
cartForm ? . addEventListener ( 'submit' , function ( e ) {
cartForm ? . addEventListener ( 'submit' , function ( e ) {
e . preventDefault ( ) ;
e . preventDefault ( ) ;
e . stopImmediatePropagation ( ) ;
e . stopImmediatePropagation ( ) ;
@ -25,7 +25,7 @@ document.addEventListener("DOMContentLoaded", (event) => {
function cartCheckout ( formElement ) {
function cartCheckout ( formElement ) {
if ( ! formElement ) return ;
if ( ! formElement ) return ;
const formData = new FormData ( formElement ) ;
const formData = new FormData ( formElement ) ;
const submitButton = formElement . querySelector ( 'button[type=submit]' ) ;
const submitButton = formElement . querySelector ( 'button[type=submit]' ) ;
@ -47,7 +47,8 @@ function addToCart(formElement) {
submitButton . classList . add ( 'is-loading' ) ;
submitButton . classList . add ( 'is-loading' ) ;
}
}
fetch ( window . location . href , {
fetch ( formElement . getAttribute ( 'action' ) , { // чтоб работало по action формы
// fetch(window.location.href, {
method : 'POST' ,
method : 'POST' ,
body : formData
body : formData
} )
} )
@ -145,7 +146,7 @@ function handleConsultationForm(formElement) {
submitButton . classList . remove ( 'is-loading' ) ;
submitButton . classList . remove ( 'is-loading' ) ;
}
}
} ) ;
} ) ;
}
}
// search-history.js
// search-history.js
@ -192,7 +193,7 @@ function updateHistoryUI(history, containerId, inputId) {
} ) ;
} ) ;
// Повторить поиск по истории
// Повторить поиск по истории
container . querySelectorAll ( '.header__search-btn--again' ) . forEach ( btn => {
container . querySelectorAll ( '.header__search-btn--again' ) . forEach ( btn => {
btn . addEventListener ( 'click' , function ( e ) {
btn . addEventListener ( 'click' , function ( e ) {
e . preventDefault ( ) ;
e . preventDefault ( ) ;
const val = this . getAttribute ( 'data-value' ) ;
const val = this . getAttribute ( 'data-value' ) ;
const input = document . getElementById ( inputId ) ;
const input = document . getElementById ( inputId ) ;
@ -201,7 +202,7 @@ function updateHistoryUI(history, containerId, inputId) {
} ) ;
} ) ;
// Удалить элемент истории
// Удалить элемент истории
container . querySelectorAll ( '.header__search-btn--remove' ) . forEach ( btn => {
container . querySelectorAll ( '.header__search-btn--remove' ) . forEach ( btn => {
btn . addEventListener ( 'click' , function ( e ) {
btn . addEventListener ( 'click' , function ( e ) {
e . preventDefault ( ) ;
e . preventDefault ( ) ;
const idx = + this . getAttribute ( 'data-idx' ) ;
const idx = + this . getAttribute ( 'data-idx' ) ;
history . splice ( idx , 1 ) ;
history . splice ( idx , 1 ) ;
@ -223,12 +224,12 @@ function setupSearchHistory(formId, inputId, containerId, clearBtnId) {
const clearBtn = document . getElementById ( clearBtnId ) ;
const clearBtn = document . getElementById ( clearBtnId ) ;
if ( form && input ) {
if ( form && input ) {
form . addEventListener ( 'submit' , function ( e ) {
form . addEventListener ( 'submit' , function ( e ) {
e . preventDefault ( ) ;
e . preventDefault ( ) ;
const val = input . value . trim ( ) ;
const val = input . value . trim ( ) ;
if ( ! val ) return ;
if ( ! val ) return ;
// Не дублируем подряд одинаковые
// Не дублируем подряд одинаковые
if ( ! history . length || history [ history . length - 1 ] !== val ) {
if ( ! history . length || history [ history . length - 1 ] !== val ) {
history . push ( val ) ;
history . push ( val ) ;
if ( history . length > 10 ) history = history . slice ( - 10 ) ; // максимум 10
if ( history . length > 10 ) history = history . slice ( - 10 ) ; // максимум 10
setCookie ( containerId , JSON . stringify ( history ) ) ;
setCookie ( containerId , JSON . stringify ( history ) ) ;
@ -239,7 +240,7 @@ function setupSearchHistory(formId, inputId, containerId, clearBtnId) {
} ) ;
} ) ;
}
}
if ( clearBtn ) {
if ( clearBtn ) {
clearBtn . addEventListener ( 'click' , function ( e ) {
clearBtn . addEventListener ( 'click' , function ( e ) {
e . preventDefault ( ) ;
e . preventDefault ( ) ;
history = [ ] ;
history = [ ] ;
setCookie ( containerId , JSON . stringify ( history ) ) ;
setCookie ( containerId , JSON . stringify ( history ) ) ;
@ -277,7 +278,7 @@ function ajaxSearch(inputId, blockId, historyBlockId) {
const input = document . getElementById ( inputId ) ;
const input = document . getElementById ( inputId ) ;
const historyBlock = document . getElementById ( historyBlockId ) ;
const historyBlock = document . getElementById ( historyBlockId ) ;
if ( ! input ) return ;
if ( ! input ) return ;
input . addEventListener ( 'input' , function ( ) {
input . addEventListener ( 'input' , function ( ) {
const val = input . value . trim ( ) ;
const val = input . value . trim ( ) ;
clearSearchResults ( blockId ) ;
clearSearchResults ( blockId ) ;
if ( historyBlock ) {
if ( historyBlock ) {
@ -298,7 +299,7 @@ function ajaxSearch(inputId, blockId, historyBlockId) {
} ) ;
} ) ;
} ) ;
} ) ;
// Скрывать результаты при потере фокуса
// Скрывать результаты при потере фокуса
input . addEventListener ( 'blur' , function ( ) {
input . addEventListener ( 'blur' , function ( ) {
setTimeout ( ( ) => clearSearchResults ( blockId ) , 200 ) ;
setTimeout ( ( ) => clearSearchResults ( blockId ) , 200 ) ;
if ( historyBlock && input . value . trim ( ) . length === 0 ) {
if ( historyBlock && input . value . trim ( ) . length === 0 ) {
historyBlock . style . display = '' ;
historyBlock . style . display = '' ;
@ -310,10 +311,10 @@ function setupSearchBlockFocus(inputId, blockId) {
const input = document . getElementById ( inputId ) ;
const input = document . getElementById ( inputId ) ;
const block = document . getElementById ( blockId ) ;
const block = document . getElementById ( blockId ) ;
if ( ! input || ! block ) return ;
if ( ! input || ! block ) return ;
input . addEventListener ( 'focus' , function ( ) {
input . addEventListener ( 'focus' , function ( ) {
block . classList . add ( 'active' ) ;
block . classList . add ( 'active' ) ;
} ) ;
} ) ;
input . addEventListener ( 'blur' , function ( ) {
input . addEventListener ( 'blur' , function ( ) {
setTimeout ( ( ) => block . classList . remove ( 'active' ) , 200 ) ;
setTimeout ( ( ) => block . classList . remove ( 'active' ) , 200 ) ;
} ) ;
} ) ;
}
}
@ -323,7 +324,7 @@ function toggleSearchTopAndHistory(inputId, topId, historiesId) {
const top = document . getElementById ( topId ) ;
const top = document . getElementById ( topId ) ;
const histories = document . getElementById ( historiesId ) ;
const histories = document . getElementById ( historiesId ) ;
if ( ! input || ! top || ! histories ) return ;
if ( ! input || ! top || ! histories ) return ;
input . addEventListener ( 'input' , function ( ) {
input . addEventListener ( 'input' , function ( ) {
if ( input . value . trim ( ) . length > 0 ) {
if ( input . value . trim ( ) . length > 0 ) {
top . style . display = 'none' ;
top . style . display = 'none' ;
histories . style . display = 'none' ;
histories . style . display = 'none' ;
@ -332,7 +333,7 @@ function toggleSearchTopAndHistory(inputId, topId, historiesId) {
histories . style . display = '' ;
histories . style . display = '' ;
}
}
} ) ;
} ) ;
input . addEventListener ( 'blur' , function ( ) {
input . addEventListener ( 'blur' , function ( ) {
if ( input . value . trim ( ) . length === 0 ) {
if ( input . value . trim ( ) . length === 0 ) {
setTimeout ( ( ) => {
setTimeout ( ( ) => {
top . style . display = '' ;
top . style . display = '' ;
@ -347,7 +348,7 @@ function addToHistory(query, containerId, inputId) {
try {
try {
history = JSON . parse ( getCookie ( containerId ) || '[]' ) ;
history = JSON . parse ( getCookie ( containerId ) || '[]' ) ;
} catch ( e ) { history = [ ] ; }
} catch ( e ) { history = [ ] ; }
if ( ! history . length || history [ history . length - 1 ] !== query ) {
if ( ! history . length || history [ history . length - 1 ] !== query ) {
history . push ( query ) ;
history . push ( query ) ;
if ( history . length > 10 ) history = history . slice ( - 10 ) ;
if ( history . length > 10 ) history = history . slice ( - 10 ) ;
setCookie ( containerId , JSON . stringify ( history ) ) ;
setCookie ( containerId , JSON . stringify ( history ) ) ;
@ -356,7 +357,7 @@ function addToHistory(query, containerId, inputId) {
}
}
function setupResultClickToHistory ( inputId , containerId ) {
function setupResultClickToHistory ( inputId , containerId ) {
document . addEventListener ( 'click' , function ( e ) {
document . addEventListener ( 'click' , function ( e ) {
const target = e . target . closest ( '.search-result-item' ) ;
const target = e . target . closest ( '.search-result-item' ) ;
if ( target ) {
if ( target ) {
const input = document . getElementById ( inputId ) ;
const input = document . getElementById ( inputId ) ;
@ -370,7 +371,7 @@ function setupResultClickToHistory(inputId, containerId) {
} , true ) ;
} , true ) ;
}
}
document . addEventListener ( 'DOMContentLoaded' , function ( ) {
document . addEventListener ( 'DOMContentLoaded' , function ( ) {
setupSearchHistory ( 'header-search-form' , 'header-search-input' , 'header-search-histories' , 'header-search-clear' ) ;
setupSearchHistory ( 'header-search-form' , 'header-search-input' , 'header-search-histories' , 'header-search-clear' ) ;
setupSearchHistory ( 'mobile-search-form' , 'mobile-search-input' , 'mobile-search-histories' , 'mobile-search-clear' ) ;
setupSearchHistory ( 'mobile-search-form' , 'mobile-search-input' , 'mobile-search-histories' , 'mobile-search-clear' ) ;
ajaxSearch ( 'header-search-input' , 'header-search-history-block' , 'header-search-histories' ) ;
ajaxSearch ( 'header-search-input' , 'header-search-history-block' , 'header-search-histories' ) ;
@ -381,9 +382,14 @@ document.addEventListener('DOMContentLoaded', function() {
toggleSearchTopAndHistory ( 'mobile-search-input' , 'mobile-search-top' , 'mobile-search-histories' ) ;
toggleSearchTopAndHistory ( 'mobile-search-input' , 'mobile-search-top' , 'mobile-search-histories' ) ;
setupResultClickToHistory ( 'header-search-input' , 'header-search-histories' ) ;
setupResultClickToHistory ( 'header-search-input' , 'header-search-histories' ) ;
setupResultClickToHistory ( 'mobile-search-input' , 'mobile-search-histories' ) ;
setupResultClickToHistory ( 'mobile-search-input' , 'mobile-search-histories' ) ;
} ) ;
} ) ;
// Открытие попапа для local/components/era/calculator/templates/.default/ajax.php
function openCartPopupForProduct ( productName ) {
document . querySelector ( '.cart-popup__wrapper' ) . classList . remove ( 'cart-popup__wrapper--hidden' ) ;
document . getElementById ( 'fast_product' ) . value = productName ;
}
// From main.js
// From main.js