wishthis/src/assets/js/wish.js

153 lines
4.9 KiB
JavaScript
Raw Normal View History

2022-02-27 11:23:38 +00:00
$(function () {
/**
2022-03-04 10:03:12 +00:00
* Auto fill
2022-02-27 11:23:38 +00:00
*/
2022-03-04 10:03:12 +00:00
if ($('[name="wish_url"]').val()) {
$('.button.auto-fill').removeClass('disabled');
}
$(document).on('click', '.button.auto-fill', function () {
2022-02-27 11:23:38 +00:00
var modalAutoFill = $('.modal.auto-fill');
var modalValidate = $('.modal.validate');
2022-03-07 10:55:39 +00:00
var formWish = $('.form.wish');
var imagePreview = $('img.preview');
2022-02-27 11:23:38 +00:00
var inputTitle = $('[name="wish_title"]');
var inputDescription = $('[name="wish_description"]');
2022-03-07 10:55:39 +00:00
var inputImage = $('[name="wish_image"]');
2022-02-27 11:23:38 +00:00
var inputURL = $('[name="wish_url"]');
modalAutoFill
.modal({
2022-03-11 14:56:42 +00:00
autoShow : true,
onApprove: function() {
2022-02-27 11:23:38 +00:00
formWish.addClass('loading');
fetch('/src/api/wishes.php?wish_url=' + inputURL.val(), {
method: 'GET'
})
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
var info = response.info;
2022-02-27 11:33:02 +00:00
/**
* Prodiver name
*/
if (info.providerName) {
modalValidate.find('.providerName').text(info.providerName);
} else {
modalValidate.find('.provider').remove();
}
2022-02-27 11:23:38 +00:00
/**
* Title
*/
if (info.title) {
inputTitle.val(info.title);
}
/**
* Description
*/
if (info.description) {
inputDescription.val(info.description);
}
2022-03-07 10:55:39 +00:00
/**
* Image
*/
if (info.image) {
inputImage.val(info.image);
imagePreview.attr('src', info.image);
}
2022-02-27 11:23:38 +00:00
/**
* URL
*/
if (info.url && info.url !== inputURL.val()) {
var elementModalFetch = $('.modal.validate');
elementModalFetch.find('input.current').val(inputURL.val());
elementModalFetch.find('input.proposed').val(info.url);
elementModalFetch
.modal({
2022-03-11 14:56:42 +00:00
autoShow: true,
2022-02-27 11:23:38 +00:00
onApprove: function (buttonFetch) {
var formData = new URLSearchParams();
formData.append('wish_url_current', inputURL.val());
formData.append('wish_url_proposed', info.url);
buttonFetch.addClass('loading');
fetch('/src/api/wishes.php', {
method: 'PUT',
2022-03-22 15:15:56 +00:00
body : formData
2022-02-27 11:23:38 +00:00
})
.then(response => response.json())
.then(response => {
if (response.success) {
inputURL.val(info.url);
elementModalFetch.modal('hide');
2022-03-22 15:15:56 +00:00
$('body').toast({ message: text.toast_wish_update });
2022-02-27 11:23:38 +00:00
}
buttonFetch.removeClass('loading');
});
return false;
},
onHide: function() {
formWish.removeClass('loading');
}
2022-03-11 14:56:42 +00:00
});
2022-02-27 11:23:38 +00:00
} else {
2022-03-07 09:41:46 +00:00
$('body').toast({
2022-03-22 15:15:56 +00:00
class : 'primary',
message: text.toast_wish_save
2022-03-07 09:41:46 +00:00
});
2022-02-27 11:33:02 +00:00
2022-02-27 11:23:38 +00:00
formWish.removeClass('loading');
}
})
2022-03-05 09:50:21 +00:00
.catch(handleFetchCatch);
2022-02-27 11:23:38 +00:00
}
2022-03-11 14:56:42 +00:00
});
2022-02-27 11:23:38 +00:00
});
2022-02-27 14:07:07 +00:00
/**
* Image
*/
$(document).on('click', '.image.preview', function() {
2022-03-04 08:32:56 +00:00
var modalImage = $('.modal.preview');
2022-02-27 14:07:07 +00:00
modalImage
.modal({
2022-03-11 14:56:42 +00:00
autoShow: true,
2022-03-07 10:55:39 +00:00
onApprove: function() {
var newImageURL = modalImage.find('[name="wish_image"]').val();
2022-02-27 14:07:07 +00:00
2022-03-07 10:55:39 +00:00
$('img.preview').attr('src', newImageURL);
$('.form.wish [name="wish_image"]').val(newImageURL);
2022-02-27 14:07:07 +00:00
2022-03-07 10:55:39 +00:00
$('body').toast({
2022-03-22 15:15:56 +00:00
class : 'primary',
message: text.toast_wish_save
2022-02-27 14:07:07 +00:00
});
}
2022-03-11 14:56:42 +00:00
});
2022-02-27 14:07:07 +00:00
});
2022-03-07 10:36:51 +00:00
/**
* Priority
*/
$('.dropdown.priority').dropdown();
2022-02-27 11:23:38 +00:00
});