From 567b6e700374332136e6b7bd7b62502e52ec7ca1 Mon Sep 17 00:00:00 2001 From: Jim Safley Date: Wed, 1 May 2024 16:16:44 -0400 Subject: [PATCH 1/3] Add max attachments option to block attachments form --- application/asset/js/site-page-edit.js | 29 +++++++++++++++++++ .../src/View/Helper/BlockAttachmentsForm.php | 3 +- .../view/common/attachments-form.phtml | 2 +- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/application/asset/js/site-page-edit.js b/application/asset/js/site-page-edit.js index 66f3c28dcd..c50f780c46 100644 --- a/application/asset/js/site-page-edit.js +++ b/application/asset/js/site-page-edit.js @@ -237,6 +237,26 @@ }); }); + // Hide "Add attachment" buttons when the number of attachments equal or + // exceed the maxAttachments setting. + $('.attachments').each(function() { + var attachmentsContainer = $(this); + var attachments = attachmentsContainer.children('.attachment'); + var maxAttachments = parseInt(attachmentsContainer.data('maxAttachments'), 10); + if (maxAttachments && attachments.length >= maxAttachments) { + attachmentsContainer.children('button.attachment-add').hide(); + } + }); + + // Hide the bulk select controls when there is a maxAttachment setting. + $('#select-resource').on('o:sidebar-content-loaded', function() { + var thisSidebar = $(this); + var attachmentsContainer = $('.selecting-attachment').closest('.attachments'); + var maxAttachments = parseInt(attachmentsContainer.data('maxAttachments'), 10); + var bulkSelectControls = thisSidebar.find('.quick-select-toggle, .select-all'); + maxAttachments ? bulkSelectControls.hide() : bulkSelectControls.show(); + }); + $('#new-block button').on('click', function() { $.post( $(this).parents('#new-block').data('url'), @@ -441,6 +461,15 @@ } attachment.find('.item-title').empty().append(thumbnail).append(title); } + + // Hide the "Add attachment" button when the number of attachments + // equal or exceed the maxAttachments setting. + var attachmentsContainer = $('.selecting-attachment').closest('.attachments'); + var attachments = attachmentsContainer.children('.attachment'); + var maxAttachments = parseInt(attachmentsContainer.data('maxAttachments'), 10); + if (maxAttachments && attachments.length >= maxAttachments) { + attachmentsContainer.children('button.attachment-add').hide(); + } }); $('#blocks').on('click', '.asset-options-configure', function(e) { diff --git a/application/src/View/Helper/BlockAttachmentsForm.php b/application/src/View/Helper/BlockAttachmentsForm.php index 3670d94af3..8b0b8855a1 100644 --- a/application/src/View/Helper/BlockAttachmentsForm.php +++ b/application/src/View/Helper/BlockAttachmentsForm.php @@ -22,12 +22,13 @@ class BlockAttachmentsForm extends AbstractHelper * @return string */ public function __invoke(SitePageBlockRepresentation $block = null, $itemOnly = false, - array $itemQuery = []) + array $itemQuery = [], ?int $maxAttachments = 1) { return $this->getView()->partial('common/attachments-form', [ 'block' => $block, 'itemOnly' => (bool) $itemOnly, 'itemQuery' => $itemQuery, + 'maxAttachments' => $maxAttachments, ]); } } diff --git a/application/view/common/attachments-form.phtml b/application/view/common/attachments-form.phtml index b85a85cdbc..58c0229b54 100644 --- a/application/view/common/attachments-form.phtml +++ b/application/view/common/attachments-form.phtml @@ -18,7 +18,7 @@ $attachmentRowTemplate = ' ?>

-
+
Date: Wed, 1 May 2024 16:22:30 -0400 Subject: [PATCH 2/3] Set no-max as maxAttachments default --- application/src/View/Helper/BlockAttachmentsForm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/View/Helper/BlockAttachmentsForm.php b/application/src/View/Helper/BlockAttachmentsForm.php index 8b0b8855a1..d6675b20ab 100644 --- a/application/src/View/Helper/BlockAttachmentsForm.php +++ b/application/src/View/Helper/BlockAttachmentsForm.php @@ -22,7 +22,7 @@ class BlockAttachmentsForm extends AbstractHelper * @return string */ public function __invoke(SitePageBlockRepresentation $block = null, $itemOnly = false, - array $itemQuery = [], ?int $maxAttachments = 1) + array $itemQuery = [], ?int $maxAttachments = null) { return $this->getView()->partial('common/attachments-form', [ 'block' => $block, From b58bd58e21ed558a62ff915646918acf3f672ec1 Mon Sep 17 00:00:00 2001 From: Jim Safley Date: Mon, 6 May 2024 16:27:51 -0400 Subject: [PATCH 3/3] Move duplicate code to a function --- application/asset/js/site-page-edit.js | 36 ++++++++++++++------------ 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/application/asset/js/site-page-edit.js b/application/asset/js/site-page-edit.js index c50f780c46..e38c2df3c7 100644 --- a/application/asset/js/site-page-edit.js +++ b/application/asset/js/site-page-edit.js @@ -197,6 +197,22 @@ }); } + /** + * Enable max attachments. + * + * Hides the "Add attachment" button when the number of attachments equals + * or exceeds the maxAttachments setting. + * + * @param object attachmentsContainer + */ + function enableMaxAttachments(attachmentsContainer) { + var attachments = attachmentsContainer.children('.attachment'); + var maxAttachments = parseInt(attachmentsContainer.data('maxAttachments'), 10); + if (maxAttachments && attachments.length >= maxAttachments) { + attachmentsContainer.children('button.attachment-add').hide(); + } + } + $(document).ready(function () { var blockIndex = 0; @@ -237,15 +253,9 @@ }); }); - // Hide "Add attachment" buttons when the number of attachments equal or - // exceed the maxAttachments setting. + // Enable max attachmnets. $('.attachments').each(function() { - var attachmentsContainer = $(this); - var attachments = attachmentsContainer.children('.attachment'); - var maxAttachments = parseInt(attachmentsContainer.data('maxAttachments'), 10); - if (maxAttachments && attachments.length >= maxAttachments) { - attachmentsContainer.children('button.attachment-add').hide(); - } + enableMaxAttachments($(this)); }); // Hide the bulk select controls when there is a maxAttachment setting. @@ -462,14 +472,8 @@ attachment.find('.item-title').empty().append(thumbnail).append(title); } - // Hide the "Add attachment" button when the number of attachments - // equal or exceed the maxAttachments setting. - var attachmentsContainer = $('.selecting-attachment').closest('.attachments'); - var attachments = attachmentsContainer.children('.attachment'); - var maxAttachments = parseInt(attachmentsContainer.data('maxAttachments'), 10); - if (maxAttachments && attachments.length >= maxAttachments) { - attachmentsContainer.children('button.attachment-add').hide(); - } + // Enable max attachmnets. + enableMaxAttachments($('.selecting-attachment').closest('.attachments')); }); $('#blocks').on('click', '.asset-options-configure', function(e) {