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

259 lines
8.2 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.'
})
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-02-22 13:00:41 +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
$('[name="wishlist_delete_id"]').val(wishlistValue);
if (wishlistValue) {
2022-02-22 09:40:04 +00:00
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-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 12:02:32 +00:00
/**
* Cards
*/
$('.wishlist-cards').html(wishlists[wishlistIndex].cards);
2022-02-23 12:14:50 +00:00
/**
* Generate cache
*/
2022-02-23 13:48:32 +00:00
var timerInterval = 1200;
2022-02-23 12:14:50 +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"]');
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);
var href = card.find('.content [href]').prop('href');
var product_id = card.data('id');
card.addClass('loading');
card.attr('data-cache', true);
fetch('/src/api/cache.php?product_id=' + product_id + '&url=' + href, {
method: 'GET'
})
.then(response => response.json())
.then(response => {
if (response.success) {
var info = response.data;
/**
* Elements
*/
var elementImage = card.children('.image');
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-23 15:46:55 +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
}
}
/**
* Header
*/
var elementContentHeader = elementContent.children('.header');
var elementContentHeaderTitle = elementContentHeader.children('a');
/** Favicon */
if (info.favicon) {
elementContentHeader.prepend(
'<img src="' + info.favicon + '" loading="lazy">'
);
}
/** 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(
'<div class="description">' + info.description + '</div>'
);
}
}
/**
* Details
*/
if (info.publishedTime || info.providerName) {
if (!elementDetails.length) {
elementButtons.before().append(
'<div class="extra content details"></div>'
);
if (info.publishedTime) {
elementContent.children('.extra.content.details').append(
'<span class="right floated">' + info.publishedTime + '</span>'
);
}
if (info.providerName) {
elementContent.children('.extra.content.details').append(
info.providerName
);
}
}
}
/**
* Finish
*/
card.removeClass('loading');
}
});
}
/**
* Refresh
*/
$(document).on('click', '.ui.button.refresh', function(event) {
var card = $(event.currentTarget).closest('.ui.card');
console.log(card);
generateCacheCard(card);
});
2022-02-22 09:40:04 +00:00
/**
* Delete Wishlist
*/
2022-02-22 13:00:41 +00:00
$(document).on('submit', '.wishlist-delete', function(event) {
2022-01-17 15:06:17 +00:00
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',
2022-02-22 13:00:41 +00:00
data: {
2022-01-17 15:06:17 +00:00
wishlistID: wishlistValue
},
2022-02-22 13:00:41 +00:00
on: 'now',
onSuccess: function(response, wishlists) {
$('.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');
window.history.pushState({}, '', '/?' + urlParams.toString());
2022-01-17 15:06:17 +00:00
2022-02-22 13:00:41 +00:00
$('.ui.dropdown.wishlists').api({
action: 'get wishlists',
method: 'GET',
on: 'now'
});
2022-01-17 15:06:17 +00:00
}
});
}
})
.modal('show');
}
event.preventDefault();
2022-01-17 13:18:01 +00:00
});
});