customElements.define('favorite-button', class extends HTMLElement {
static observedAttributes = ['text', 'liked'];
constructor () {
super();
this.apiCheck = this.getAttribute('api-check');
if (!this.apiCheck) return;
this.apiLike = this.getAttribute('api-like');
this.apiUnlike = this.getAttribute('api-unlike');
this.isLiked = false;
this.tippyData = {
'false': 'Добавить в избранное',
'true': 'Удалить из избранного'
}
this.alternativeDesign = this.hasAttribute('alternative');
this.alternativeSvg = {
like: '<path d="m20 35.58-2.42-2.16c-2.8-2.53-5.12-4.71-6.95-6.55a46.04 46.04 0 0 1-4.38-4.93 15.65 15.65 0 0 1-2.27-4.02 11.25 11.25 0 0 1-.65-3.75c0-2.61.88-4.8 2.63-6.54A8.88 8.88 0 0 1 12.5 5 9.88 9.88 0 0 1 20 8.5 9.88 9.88 0 0 1 27.5 5c2.61 0 4.8.88 6.54 2.63a8.88 8.88 0 0 1 2.63 6.54c0 1.27-.22 2.52-.65 3.75a15.63 15.63 0 0 1-2.27 4.02 46.03 46.03 0 0 1-4.38 4.93 188.43 188.43 0 0 1-6.95 6.55L20 35.58Z"/>',
unlike: '<path d="m20 35.58-2.42-2.16a188.5 188.5 0 0 1-6.95-6.55 46.04 46.04 0 0 1-4.38-4.93 15.65 15.65 0 0 1-2.27-4.02 11.25 11.25 0 0 1-.65-3.75c0-2.61.88-4.8 2.63-6.54A8.88 8.88 0 0 1 12.5 5 9.88 9.88 0 0 1 20 8.5 9.88 9.88 0 0 1 27.5 5c2.61 0 4.8.88 6.54 2.63a8.88 8.88 0 0 1 2.63 6.54c0 1.27-.22 2.52-.65 3.75a15.63 15.63 0 0 1-2.27 4.02 46.05 46.05 0 0 1-4.38 4.93 188.52 188.52 0 0 1-6.95 6.55L20 35.58Zm0-4.5c2.67-2.39 4.86-4.43 6.58-6.14 1.73-1.7 3.09-3.2 4.09-4.46 1-1.26 1.7-2.39 2.08-3.38.39-.98.58-1.96.58-2.93a5.65 5.65 0 0 0-5.83-5.83c-1.3 0-2.51.36-3.63 1.1a5.67 5.67 0 0 0-2.29 2.81h-3.16a5.67 5.67 0 0 0-2.3-2.81 6.45 6.45 0 0 0-3.62-1.1 5.65 5.65 0 0 0-5.83 5.83c0 .97.2 1.95.58 2.94.39.98 1.08 2.1 2.08 3.37a52.1 52.1 0 0 0 4.09 4.46c1.72 1.7 3.91 3.76 6.58 6.14Z"/>'
};
this.isAuthUser = userData.user;
this.createHeart();
this.setAttribute('liked', false);
if (this.isAuthUser) {
this.getLikeStatus().then(res=>{
if (res.needAuth) {
return
};
this.isLiked = res.is_in_favourites;
this.setAttribute('liked', this.isLiked);
if (res.is_in_favourites) {
if (this.alternativeDesign) {
this.button.classList.toggle('like');
this.button.classList.toggle('unlike');
} else {
this.button.classList.add('active');
}
if (this.isLiked) {
this.button.title = this.tippyData[this.isLiked];
this.button.setAttribute('data-tippy-main', this.tippyData[this.isLiked]);
document.dispatchEvent(new CustomEvent('tippy:update', {detail:{
element: this.button,
text: this.tippyData[this.isLiked]
}}));
}
}
});
}
}
createHeart() {
this.button = this.querySelector('button');
if (this.alternativeDesign) {
this.button.className = `btn btn-favorite favorite ${this.isLiked ? 'like' : 'unlike'}`;
this.button.innerHTML = `<svg class="btn-favorite-icon" width="40" height="40" fill="none">
<path data-like d="m20 35.58-2.42-2.16c-2.8-2.53-5.12-4.71-6.95-6.55a46.04 46.04 0 0 1-4.38-4.93 15.65 15.65 0 0 1-2.27-4.02 11.25 11.25 0 0 1-.65-3.75c0-2.61.88-4.8 2.63-6.54A8.88 8.88 0 0 1 12.5 5 9.88 9.88 0 0 1 20 8.5 9.88 9.88 0 0 1 27.5 5c2.61 0 4.8.88 6.54 2.63a8.88 8.88 0 0 1 2.63 6.54c0 1.27-.22 2.52-.65 3.75a15.63 15.63 0 0 1-2.27 4.02 46.03 46.03 0 0 1-4.38 4.93 188.43 188.43 0 0 1-6.95 6.55L20 35.58Z"/>
<path data-unlike d="m20 35.58-2.42-2.16a188.5 188.5 0 0 1-6.95-6.55 46.04 46.04 0 0 1-4.38-4.93 15.65 15.65 0 0 1-2.27-4.02 11.25 11.25 0 0 1-.65-3.75c0-2.61.88-4.8 2.63-6.54A8.88 8.88 0 0 1 12.5 5 9.88 9.88 0 0 1 20 8.5 9.88 9.88 0 0 1 27.5 5c2.61 0 4.8.88 6.54 2.63a8.88 8.88 0 0 1 2.63 6.54c0 1.27-.22 2.52-.65 3.75a15.63 15.63 0 0 1-2.27 4.02 46.05 46.05 0 0 1-4.38 4.93 188.52 188.52 0 0 1-6.95 6.55L20 35.58Zm0-4.5c2.67-2.39 4.86-4.43 6.58-6.14 1.73-1.7 3.09-3.2 4.09-4.46 1-1.26 1.7-2.39 2.08-3.38.39-.98.58-1.96.58-2.93a5.65 5.65 0 0 0-5.83-5.83c-1.3 0-2.51.36-3.63 1.1a5.67 5.67 0 0 0-2.29 2.81h-3.16a5.67 5.67 0 0 0-2.3-2.81 6.45 6.45 0 0 0-3.62-1.1 5.65 5.65 0 0 0-5.83 5.83c0 .97.2 1.95.58 2.94.39.98 1.08 2.1 2.08 3.37a52.1 52.1 0 0 0 4.09 4.46c1.72 1.7 3.91 3.76 6.58 6.14Z"/>
</svg>`;
} else {
this.button.className = `btn btn-profile btn-profile--like ${this.isLiked ? 'active' : ''}`;
this.button.innerHTML = `<svg class="like-fill" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none"><path fill="#FF3F40" fill-rule="evenodd" d="M12 4.915c1.09-1.28 2.76-2.09 4.5-2.09 3.08 0 5.5 2.42 5.5 5.5 0 3.777-3.394 6.855-8.537 11.519l-.013.011-1.45 1.32-1.45-1.31-.04-.036C5.384 15.17 2 12.095 2 8.325c0-3.08 2.42-5.5 5.5-5.5 1.74 0 3.41.81 4.5 2.09Z" clip-rule="evenodd"/></svg>
<svg class="like-border" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none"><path fill-rule="evenodd" d="M12 4.915c1.09-1.28 2.76-2.09 4.5-2.09 3.08 0 5.5 2.42 5.5 5.5 0 3.777-3.394 6.855-8.537 11.519l-.013.011-1.45 1.32-1.45-1.31-.04-.036C5.384 15.17 2 12.095 2 8.325c0-3.08 2.42-5.5 5.5-5.5 1.74 0 3.41.81 4.5 2.09Zm0 13.56.1-.1c4.76-4.31 7.9-7.16 7.9-10.05 0-2-1.5-3.5-3.5-3.5-1.54 0-3.04.99-3.56 2.36h-1.87c-.53-1.37-2.03-2.36-3.57-2.36-2 0-3.5 1.5-3.5 3.5 0 2.89 3.14 5.74 7.9 10.05l.1.1Z" clip-rule="evenodd"/></svg>`
;
}
this.button.addEventListener('click', this);
this.button.title = this.tippyData[this.isLiked];
this.button.setAttribute('data-tippy-main', this.tippyData[this.isLiked]);
this.append(this.button);
// animation-heart-beat
const profileAvatar = this.button.closest('[data-favorite-button]');
if (!profileAvatar) return;
profileAvatar.addEventListener('mouseover', () => {
this.button.classList.add('animation-heart-beat');
});
profileAvatar.addEventListener('mouseout', () => {
this.button.classList.remove('animation-heart-beat');
})
}
handleEvent (event) {
event.preventDefault();
if (event.type !== 'click') return;
if (this.alternativeDesign) {
this.button.classList.toggle('like');
this.button.classList.toggle('unlike');
} else {
this.button.classList.toggle('active');
}
if (!this.isAuthUser) {
window.location = this.getAttribute('registration-url');
return;
}
fetch(this.isLiked ? this.apiUnlike : this.apiLike, {
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
})
.then(res => {
this.button.classList.remove('is-loading');
if (res.status !== 200) return
return res.json();
})
.then(data => {
if (!data.success) return;
if (this.alternativeDesign) {
this.button.classList.toggle('unlike', this.isLiked)
this.isLiked = !this.isLiked;
this.button.classList.toggle('like', this.isLiked)
} else {
this.isLiked = !this.isLiked;
this.button.classList.toggle('active', this.isLiked)
}
this.button.title = this.tippyData[this.isLiked];
this.button.setAttribute('data-tippy-main', this.tippyData[this.isLiked]);
document.dispatchEvent(new CustomEvent('tippy:update', {detail:{
element: this.button,
text: this.tippyData[this.isLiked]
}}));
})
}
getLikeStatus() {
return fetch(this.apiCheck, {
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
})
.then(res => {
if (res.status !== 200 || res.redirected) {
return {needAuth: true}
}
return res.json();
})
.then(data => {
return data;
})
}
//attributeChangedCallback (name, oldValue, newValue) {
//console.log('attribute changed', name, oldValue, newValue, this);
//}
});