kollapsminoriteten/wp-content/plugins/simple-user-avatar/admin/js/scripts.js

99 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

2025-02-28 08:42:11 +01:00
(function($) {
2022-01-14 13:13:03 +01:00
'use strict';
2022-04-02 10:26:41 +02:00
// Tags name
var tagAttachmentId = 'input[name="' + sua_obj.input_name + '"]';
var tagAttachmentAvatar = '.sua-attachment-avatar';
var tagAttachmentDesc = '#sua-attachment-description';
var tagButtonAdd = '#btn-media-add';
var tagButtonRemove = '#btn-media-remove';
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
// jQuery elements by tags
2025-02-28 08:42:11 +01:00
var elAttachmentId = $(tagAttachmentId);
var elAttachmentAvatar = $(tagAttachmentAvatar);
var elAttachmentDesc = $(tagAttachmentDesc);
var elButtonAdd = $(tagButtonAdd);
var elButtonRemove = $(tagButtonRemove);
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
// WordPress default media sizes
2025-02-28 08:42:11 +01:00
var WPMediaSizes = ['full', 'large', 'medium', 'thumbnail'];
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
// Get default Src and default SrcSet
var defaultSrc = sua_obj.default_avatar_src;
var defaultSrcSet = sua_obj.default_avatar_srcset;
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
/*
* Update attachment
*
* @since 3.6
* @return void
*/
2025-02-28 08:42:11 +01:00
function updateAttachment(attachmentSrc = '', attachmentSrcSet = '', attachmentId = null) {
2022-04-02 10:26:41 +02:00
// Change the image attributes
elAttachmentAvatar.attr({
'src': attachmentSrc,
'srcset': attachmentSrcSet
});
// Set attachment ID value
2025-02-28 08:42:11 +01:00
elAttachmentId.val(attachmentId === null ? '' : parseInt(attachmentId));
2022-04-02 10:26:41 +02:00
// Toggle class hidden
2025-02-28 08:42:11 +01:00
elAttachmentDesc.toggleClass('hidden');
elButtonRemove.toggleClass('hidden');
2022-04-02 10:26:41 +02:00
2022-01-14 13:13:03 +01:00
}
/*
2022-04-02 10:26:41 +02:00
* Init functions
2022-01-14 13:13:03 +01:00
*
2022-04-02 10:26:41 +02:00
* @since 2.8
2022-01-14 13:13:03 +01:00
*/
2022-04-02 10:26:41 +02:00
$(function() {
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
// Set click functions
2022-01-14 13:13:03 +01:00
$(document)
2025-02-28 08:42:11 +01:00
.on('click', tagButtonAdd, function() {
2022-01-14 13:13:03 +01:00
// Open WordPress Media Library
2022-04-02 10:26:41 +02:00
wp.media.editor.open();
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
// WP Media Editor function
2025-02-28 08:42:11 +01:00
wp.media.editor.send.attachment = function(props, attachment) {
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
// Set attachment Src to default URL
var attachmentSrc = attachment.url;
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
// If there is a smaller version I use it
2025-02-28 08:42:11 +01:00
for (const WPMediaSize of WPMediaSizes) {
if (typeof attachment.sizes[WPMediaSize] !== 'undefined' && typeof attachment.sizes[WPMediaSize].url !== 'undefined') {
2022-04-02 10:26:41 +02:00
attachmentSrc = attachment.sizes[WPMediaSize].url;
}
}
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
// Update Attachment
2025-02-28 08:42:11 +01:00
updateAttachment(attachmentSrc, attachmentSrc, attachment.id);
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
}
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
})
2025-02-28 08:42:11 +01:00
.on('click', tagButtonRemove, function() {
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
// Update Attachment
2025-02-28 08:42:11 +01:00
updateAttachment(defaultSrc, defaultSrcSet);
2022-01-14 13:13:03 +01:00
})
2025-02-28 08:42:11 +01:00
.on('click', tagAttachmentAvatar, function() {
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
// Trigger to add button
2025-02-28 08:42:11 +01:00
elButtonAdd.trigger('click');
2022-01-14 13:13:03 +01:00
});
});
2025-02-28 08:42:11 +01:00
})(jQuery);