wishthis/src/assets/js/wishlists.js

552 lines
18 KiB
JavaScript
Raw Normal View History

2022-02-24 12:46:29 +00:00
$(function () {
2022-02-22 10:44:43 +00:00
/**
* Get Wishlists
*/
var wishlists = [];
function wishlistsRefresh() {
$('.ui.dropdown.wishlists').api({
action: 'get wishlists',
method: 'GET',
on: 'now',
onSuccess: function (response, element, xhr) {
wishlists = response.results;
element.dropdown({
values: wishlists,
placeholder: 'No wishlist selected.'
})
2022-02-22 10:44:43 +00:00
2022-02-26 14:59:59 +00:00
if ($_GET.wishlist) {
element.dropdown('set selected', $_GET.wishlist);
} else {
if (wishlists[0]) {
element.dropdown('set selected', wishlists[0].value);
}
2022-02-22 10:44:43 +00:00
}
}
});
}
wishlistsRefresh();
2022-02-22 10:44:43 +00:00
2022-01-17 15:06:17 +00:00
/**
2022-02-22 09:40:04 +00:00
* Selection
2022-01-17 15:06:17 +00:00
*/
2022-02-25 13:28:23 +00:00
var progress = $('.ui.progress');
2022-02-24 12:46:29 +00:00
$(document).on('change', '.ui.dropdown.wishlists', function () {
2022-01-17 15:06:17 +00:00
var wishlistValue = $('.ui.dropdown.wishlists').dropdown('get value');
2022-02-22 10:44:43 +00:00
var wishlistIndex = $('.ui.dropdown.wishlists select').prop('selectedIndex') - 1;
2022-01-17 15:06:17 +00:00
2022-02-25 13:28:23 +00:00
progress.progress('reset');
progress.addClass('indeterminate');
2022-01-17 15:06:17 +00:00
$('[name="wishlist_delete_id"]').val(wishlistValue);
if (wishlistValue) {
2022-02-26 14:59:59 +00:00
$_GET.wishlist = wishlistValue;
2022-01-18 14:19:27 +00:00
2022-02-22 10:44:43 +00:00
$('.wishlist-share').attr('href', '/?wishlist=' + wishlists[wishlistIndex].hash);
2022-02-26 20:36:50 +00:00
$('.button.wishlist-wish-add').removeClass('disabled');
2022-02-25 09:24:13 +00:00
$('.button.wishlist-share').removeClass('disabled');
2022-01-17 15:06:17 +00:00
$('.wishlist-delete button').removeClass('disabled');
2022-02-26 15:22:22 +00:00
/** Update URL */
urlParams.set('wishlist', wishlistValue);
fetch('/src/api/url.php?url=' + btoa(urlParams.toString()), {
method: 'GET'
})
.then(response => response.json())
.then(response => {
if (response.success) {
window.history.pushState({}, '', response.data.url);
}
});
2022-01-17 15:06:17 +00:00
} else {
2022-02-26 20:36:50 +00:00
$('.button.wishlist-wish-add').addClass('disabled');
2022-02-25 09:24:13 +00:00
$('.button.wishlist-share').addClass('disabled');
2022-01-17 15:06:17 +00:00
$('.wishlist-delete button').addClass('disabled');
}
2022-02-22 12:02:32 +00:00
/**
* Cards
*/
if (wishlistIndex >= 0) {
$('.wishlist-cards').html(wishlists[wishlistIndex].cards);
} else {
$('.wishlist-cards').html('');
}
2022-02-23 12:14:50 +00:00
/**
* Generate cache
*/
2022-02-25 13:28:23 +00:00
var cards = $('.ui.card[data-cache="false"]');
if (cards.length > 0) {
progress.slideDown();
progress.removeClass('indeterminate');
progress.progress({
total: cards.length
});
} else {
progress.slideUp();
}
2022-02-23 13:48:32 +00:00
var timerInterval = 1200;
2022-02-24 12:46:29 +00:00
var timerCache = setTimeout(
2022-02-23 14:53:37 +00:00
function generateCacheCards() {
2022-02-23 12:14:50 +00:00
var cards = $('.ui.card[data-cache="false"]');
2022-02-24 12:46:29 +00:00
cards.each(function (index, card) {
2022-02-23 14:53:37 +00:00
generateCacheCard(card);
2022-02-23 12:14:50 +00:00
2022-02-23 13:48:32 +00:00
if (index >= 0) {
return false;
}
2022-02-23 12:14:50 +00:00
});
2022-02-23 13:48:32 +00:00
if (cards.length > 0) {
2022-02-23 14:53:37 +00:00
setTimeout(generateCacheCards, timerInterval);
2022-02-23 13:48:32 +00:00
}
2022-02-23 12:14:50 +00:00
},
2022-02-23 13:48:32 +00:00
0
2022-02-23 12:14:50 +00:00
);
2022-01-17 15:06:17 +00:00
});
2022-02-23 14:53:37 +00:00
function generateCacheCard(card) {
card = $(card);
2022-02-26 20:36:50 +00:00
var href = card.find('.content [href]').prop('href');
var wish_id = card.data('id');
var refresh = card.find('button.refresh');
2022-02-23 14:53:37 +00:00
card.addClass('loading');
card.attr('data-cache', true);
2022-02-26 20:36:50 +00:00
fetch('/src/api/cache.php?wish_id=' + wish_id + '&wish_url=' + href, {
2022-02-23 14:53:37 +00:00
method: 'GET'
})
.then(response => response.json())
.then(response => {
if (response.success) {
var info = response.data;
/**
* Elements
*/
2022-02-24 12:46:29 +00:00
var elementImage = card.children('.image');
2022-02-23 14:53:37 +00:00
var elementContent = card.children('.content').first();
var elementDetails = card.children('.extra.content.details');
var elementButtons = card.children('.extra.content.buttons');
/**
* Image
*/
if (info.image) {
if (!elementImage.length) {
card.prepend(
'<div class="image">' +
2022-02-25 10:52:33 +00:00
'<img class="preview" src="' + info.image + '" loading="lazy">' +
2022-02-23 14:53:37 +00:00
'</div>'
);
2022-02-23 15:46:55 +00:00
} else {
elementImage.children('img').attr('src', info.image);
2022-02-23 14:53:37 +00:00
}
}
2022-02-23 16:03:39 +00:00
/** Favicon */
if (info.favicon) {
var elementFavicon = elementImage.children('img.favicon');
if (!elementFavicon.length) {
elementImage.children().first().after(
'<img class="favicon" src="' + info.favicon + '" loading="lazy">'
);
} else {
elementFavicon.attr('src', info.favicon);
}
}
/** Provider name */
if (info.providerName) {
2022-02-23 17:08:50 +00:00
var elementProviderName = elementImage.children('span.provider');
if (!elementProviderName.length) {
$('<span class="provider">' + info.providerName + '</span>').insertBefore(elementImage.children().last());
} else {
elementProviderName.text(info.providerName);
}
2022-02-23 16:03:39 +00:00
}
2022-02-23 14:53:37 +00:00
/**
* Header
*/
2022-02-24 12:46:29 +00:00
var elementContentHeader = elementContent.children('.header');
2022-02-23 14:53:37 +00:00
var elementContentHeaderTitle = elementContentHeader.children('a');
/** Title */
if (info.title) {
elementContentHeaderTitle.text(info.title);
}
/**
* Meta
*/
var elementContentMeta = elementContent.children('.meta');
if (info.keywords.length) {
if (!elementContentMeta.length) {
elementContent.append(
'<div class="meta">' + info.keywords.join(', ') + '</div>'
);
}
}
/**
* Description
*/
var elementContentDescription = elementContent.children('.description');
if (info.description) {
if (!elementContentDescription.length) {
elementContent.append(
2022-02-25 10:52:33 +00:00
'<div class="description">' + info.description + '</div>' +
'<div class="description-fade"></div>'
2022-02-23 14:53:37 +00:00
);
}
}
/**
* Finish
*/
card.removeClass('loading');
2022-02-25 13:28:23 +00:00
progress.progress('increment');
2022-02-23 14:53:37 +00:00
}
2022-02-23 16:03:39 +00:00
refresh.removeClass('working');
2022-02-23 14:53:37 +00:00
});
}
/**
* Refresh
*/
2022-02-24 12:46:29 +00:00
$(document).on('click', '.ui.button.refresh', function (event) {
2022-02-23 16:03:39 +00:00
var button = $(event.currentTarget);
2022-02-24 12:46:29 +00:00
var card = button.closest('.ui.card');
2022-02-23 14:53:37 +00:00
2022-02-23 16:03:39 +00:00
button.addClass('working');
2022-02-23 14:53:37 +00:00
generateCacheCard(card);
});
2022-02-22 09:40:04 +00:00
/**
* Delete Wishlist
*/
2022-02-24 12:46:29 +00:00
$(document).on('submit', '.wishlist-delete', function (event) {
2022-02-25 09:24:13 +00:00
event.preventDefault();
2022-01-17 15:06:17 +00:00
var wishlistValue = $('.ui.dropdown.wishlists').dropdown('get value');
if (wishlistValue) {
2022-02-25 09:24:13 +00:00
var modalDefault = $('.ui.modal.default');
modalDefault
2022-01-17 15:06:17 +00:00
.modal({
title: 'Really delete?',
class: 'tiny',
content: 'Do you really want to delete the wishlist <strong>' + $('.ui.dropdown.wishlists').dropdown('get text') + '</strong>?',
actions: [
{
text: 'Yes, delete',
class: 'approve red'
},
{
text: 'No, keep',
class: 'deny'
},
],
2022-02-25 09:24:13 +00:00
onApprove: function (buttonApprove) {
buttonApprove.addClass('loading');
2022-01-17 15:06:17 +00:00
$('.ui.dropdown.wishlists').api({
action: 'delete wishlist',
method: 'DELETE',
2022-02-24 12:46:29 +00:00
data: {
2022-01-17 15:06:17 +00:00
wishlistID: wishlistValue
},
2022-02-24 12:46:29 +00:00
on: 'now',
onSuccess: function (response, wishlists) {
2022-02-22 13:00:41 +00:00
$('.wishlist-cards .column').fadeOut();
2022-01-17 15:06:17 +00:00
2022-02-22 13:00:41 +00:00
wishlists.dropdown('clear');
2022-01-17 15:06:17 +00:00
2022-02-22 13:00:41 +00:00
urlParams.delete('wishlist');
2022-01-17 15:06:17 +00:00
2022-02-25 09:24:13 +00:00
$('body').toast({
class: 'success',
showIcon: 'check',
message: 'Wishlist successfully deleted.'
2022-02-22 13:00:41 +00:00
});
2022-02-25 09:24:13 +00:00
wishlistsRefresh();
modalDefault.modal('hide');
2022-01-17 15:06:17 +00:00
}
});
2022-02-25 09:24:13 +00:00
/**
* Return false is currently not working.
*
* @version 2.8.8
* @see https://github.com/fomantic/Fomantic-UI/issues/2105
*/
return false;
2022-01-17 15:06:17 +00:00
}
})
.modal('show');
}
2022-01-17 13:18:01 +00:00
});
2022-02-24 12:46:29 +00:00
/**
2022-02-26 20:36:50 +00:00
* Delete Wish
2022-02-24 12:46:29 +00:00
*/
$(document).on('click', '.ui.button.delete', function () {
2022-02-25 09:24:13 +00:00
var button = $(this);
var card = button.closest('.ui.card');
var column = card.closest('.column');
var modalDefault = $('.ui.modal.default');
2022-02-24 12:46:29 +00:00
2022-02-25 09:24:13 +00:00
modalDefault
2022-02-24 12:46:29 +00:00
.modal({
title: 'Really delete?',
2022-02-26 20:36:50 +00:00
content: '<p>Would you really like to delete to this wish? It will be gone forever.</p>',
2022-02-24 12:46:29 +00:00
class: 'tiny',
actions: [
{
text: 'Yes, delete',
class: 'approve primary'
},
{
text: 'Cancel'
}
],
2022-02-25 09:24:13 +00:00
onApprove: function (buttonApprove) {
buttonApprove.addClass('loading');
2022-02-24 12:46:29 +00:00
/**
2022-02-26 20:36:50 +00:00
* Delete wish
2022-02-24 12:46:29 +00:00
*/
button.api({
2022-02-26 20:36:50 +00:00
action: 'delete wish',
2022-02-24 12:46:29 +00:00
method: 'DELETE',
data: {
2022-02-26 20:36:50 +00:00
wish_id: card.data('id'),
2022-02-24 12:46:29 +00:00
},
on: 'now',
onSuccess: function () {
column.fadeOut();
2022-02-25 09:24:13 +00:00
$('body').toast({
class: 'success',
showIcon: 'check',
2022-02-26 20:36:50 +00:00
message: 'Wish successfully deleted.'
2022-02-25 09:24:13 +00:00
});
wishlistsRefresh();
2022-02-25 09:24:13 +00:00
modalDefault.modal('hide');
2022-02-24 12:46:29 +00:00
},
});
2022-02-25 09:24:13 +00:00
/**
* Return false is currently not working.
*
* @version 2.8.8
* @see https://github.com/fomantic/Fomantic-UI/issues/2105
*/
return false;
2022-02-24 12:46:29 +00:00
}
})
.modal('show');
});
/**
2022-02-26 20:36:50 +00:00
* Add wish
2022-02-24 12:46:29 +00:00
*/
2022-02-26 20:36:50 +00:00
$(document).on('click', '.button.wishlist-wish-add', function () {
var modalWishlistWishAdd = $('.ui.modal.wishlist-wish-add');
2022-02-25 09:24:13 +00:00
2022-02-26 20:36:50 +00:00
modalWishlistWishAdd.find('[name="wishlist_id"]').val($('.ui.dropdown.wishlists').dropdown('get value'));
modalWishlistWishAdd
2022-02-24 12:46:29 +00:00
.modal({
2022-02-26 23:39:08 +00:00
onApprove: function (buttonAdd) {
buttonAdd.addClass('loading');
2022-02-24 12:46:29 +00:00
2022-02-26 23:39:08 +00:00
var form = $('.form.wishlist-wish-add');
2022-02-26 22:15:03 +00:00
var formData = new URLSearchParams(new FormData(form[0]));
2022-02-24 12:46:29 +00:00
2022-02-26 20:36:50 +00:00
fetch('/src/api/wishes.php', {
2022-02-24 12:46:29 +00:00
method: 'POST',
2022-02-26 22:15:03 +00:00
body: formData
2022-02-24 12:46:29 +00:00
})
2022-02-26 22:15:03 +00:00
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
2022-02-24 12:46:29 +00:00
if (response.success) {
2022-02-25 09:24:13 +00:00
$('body').toast({
class: 'success',
showIcon: 'check',
2022-02-26 20:36:50 +00:00
message: 'Wish successfully added.'
2022-02-25 09:24:13 +00:00
});
wishlistsRefresh();
2022-02-26 20:36:50 +00:00
modalWishlistWishAdd.modal('hide');
2022-02-24 12:46:29 +00:00
}
2022-02-26 23:39:08 +00:00
buttonAdd.removeClass('loading');
2022-02-26 22:15:03 +00:00
})
.catch(function(error) {
console.log(error);
2022-02-24 12:46:29 +00:00
});
return false;
2022-02-24 12:59:07 +00:00
}
2022-02-24 12:46:29 +00:00
})
.modal('show');
});
/** Fetch */
2022-02-26 23:39:08 +00:00
$(document).on('click', '#wishlist-wish-add-url-validate', function () {
var buttonValidate = $(this);
var inputWishURL = buttonValidate.prev();
var inputURLContainer = buttonValidate.parent();
2022-02-24 12:46:29 +00:00
2022-02-26 20:36:50 +00:00
var elementModalAdd = $('.ui.modal.wishlist-wish-add');
2022-02-25 09:24:13 +00:00
var elementButtons = elementModalAdd.find('.actions .button');
2022-02-26 23:39:08 +00:00
var elementName = elementModalAdd.find('input[name="wish_name"]');
2022-02-24 12:46:29 +00:00
2022-02-26 23:39:08 +00:00
buttonValidate.addClass('disabled loading');
2022-02-24 12:46:29 +00:00
elementButtons.addClass('disabled');
2022-02-26 23:39:08 +00:00
fetch('/src/api/cache.php?wish_url=' + inputWishURL.val(), {
2022-02-24 12:46:29 +00:00
method: 'GET'
})
.then(response => response.json())
.then(response => {
if (response.success) {
var info = response.data;
/**
2022-02-26 23:39:08 +00:00
* Name
2022-02-24 12:46:29 +00:00
*/
2022-02-26 23:39:08 +00:00
if (info.title && elementName.length) {
elementName.val(info.title);
2022-02-24 12:46:29 +00:00
}
/**
* URL
*/
2022-02-26 23:39:08 +00:00
if (info.url && info.url !== inputWishURL.val()) {
2022-02-26 20:36:50 +00:00
var elementModalFetch = $('.ui.modal.wishlist-wish-fetch');
2022-02-24 12:46:29 +00:00
2022-02-26 23:39:08 +00:00
elementModalFetch.find('input.current').val(inputWishURL.val());
2022-02-24 12:46:29 +00:00
elementModalFetch.find('input.proposed').val(info.url);
2022-02-24 12:59:07 +00:00
elementButtons.addClass('disabled');
2022-02-24 12:46:29 +00:00
elementModalFetch
2022-02-24 12:59:07 +00:00
.modal({
allowMultiple: true,
closable: false,
onApprove: function (buttonFetch) {
var formData = new URLSearchParams();
2022-02-26 23:39:08 +00:00
formData.append('wish_url_current', inputWishURL.val());
2022-02-26 20:36:50 +00:00
formData.append('wish_url_proposed', info.url);
2022-02-24 12:59:07 +00:00
buttonFetch.addClass('loading');
2022-02-26 20:36:50 +00:00
fetch('/src/api/wishes.php', {
2022-02-24 12:59:07 +00:00
method: 'PUT',
body: formData
})
2022-02-24 15:07:28 +00:00
.then(response => response.json())
.then(response => {
if (response.success) {
2022-02-26 23:39:08 +00:00
inputWishURL.val(info.url);
2022-02-24 12:59:07 +00:00
2022-02-24 15:07:28 +00:00
elementModalFetch.modal('hide');
}
2022-02-24 12:59:07 +00:00
2022-02-24 15:07:28 +00:00
buttonFetch.removeClass('loading');
});
2022-02-24 12:59:07 +00:00
return false;
},
onHide: function() {
2022-02-26 23:39:08 +00:00
buttonValidate.removeClass('disabled loading');
2022-02-24 12:59:07 +00:00
elementButtons.removeClass('disabled');
}
})
.modal('show');
} else {
2022-02-26 23:39:08 +00:00
buttonValidate.removeClass('disabled loading');
2022-02-24 12:59:07 +00:00
elementButtons.removeClass('disabled');
2022-02-24 12:46:29 +00:00
}
2022-02-26 23:39:08 +00:00
inputURLContainer.attr('data-validated', 'true');
2022-02-24 12:46:29 +00:00
}
});
});
/**
* Create wishlist
*/
$(document).on('click', '.button.wishlist-create', function () {
var modalWishlistCreate = $('.ui.modal.wishlist-create');
var formWishlistCreate = modalWishlistCreate.find('.ui.form');
modalWishlistCreate
.modal({
onApprove: function (buttonCreate) {
const formData = new URLSearchParams(new FormData(formWishlistCreate[0]));
formWishlistCreate.addClass('loading');
buttonCreate.addClass('loading');
fetch('/src/api/wishlists.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(response => {
if(response.success) {
modalWishlistCreate.modal('hide');
urlParams.set('wishlist', response.data.lastInsertId);
2022-02-25 09:24:13 +00:00
$('body').toast({
class: 'success',
showIcon: 'check',
message: 'Wishlist successfully created.'
});
wishlistsRefresh();
}
})
.finally(() => {
formWishlistCreate.removeClass('loading');
buttonCreate.removeClass('loading');
});
return false;
}
})
.modal('show');
});
2022-01-17 13:18:01 +00:00
});