wishthis/src/assets/js/wishlist-view.js

119 lines
3.8 KiB
JavaScript
Raw Normal View History

2022-01-17 13:18:01 +00:00
$(function() {
2022-02-22 10:44:43 +00:00
/**
* Get Wishlists
*/
var wishlists = [];
$('.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.'
})
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.has('wishlist')) {
element.dropdown('set selected', urlParams.get('wishlist'));
} else {
if (wishlists[0]) {
element.dropdown('set selected', wishlists[0].value);
}
}
}
});
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-01-17 13:18:01 +00:00
$('.ui.dropdown.wishlists').on('change', 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
$('[name="wishlist_delete_id"]').val(wishlistValue);
if (wishlistValue) {
$('.wishlist-view').removeClass('disabled');
2022-01-18 14:19:27 +00:00
2022-02-22 09:40:04 +00:00
const urlParams = new URLSearchParams(window.location.search);
urlParams.set('wishlist', wishlistValue);
window.history.pushState({}, '', '/?' + urlParams.toString());
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-01-18 11:43:28 +00:00
$('.wishlist-share').removeClass('disabled');
2022-01-17 15:06:17 +00:00
$('.wishlist-delete button').removeClass('disabled');
} else {
2022-02-22 09:40:04 +00:00
$('.wishlist-view').addClass('disabled');
2022-01-18 11:43:28 +00:00
$('.wishlist-share').addClass('disabled');
2022-01-17 15:06:17 +00:00
$('.wishlist-delete button').addClass('disabled');
}
});
2022-02-22 09:40:04 +00:00
/**
* Delete Wishlist
*/
2022-01-17 15:06:17 +00:00
$('.wishlist-delete').on('submit', function(event) {
var wishlistValue = $('.ui.dropdown.wishlists').dropdown('get value');
if (wishlistValue) {
$('body')
.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'
},
],
onApprove: function() {
$('.ui.dropdown.wishlists').api({
action: 'delete wishlist',
method: 'DELETE',
data: {
wishlistID: wishlistValue
},
on: 'now',
onResponse: function(response) {
return response;
},
successTest: function(response) {
return response.success || false;
},
onComplete: function(response, element, xhr) {
},
onSuccess: function(response, element, xhr) {
wishlistRefresh();
},
onFailure: function(response, element, xhr) {
},
onError: function(errorMessage, element, xhr) {
},
onAbort: function(errorMessage, element, xhr) {
}
});
}
})
.modal('show');
}
event.preventDefault();
2022-01-17 13:18:01 +00:00
});
});