diff --git a/application/asset/js/site-page-edit.js b/application/asset/js/site-page-edit.js index 66f3c28dc..e38c2df3c 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,6 +253,20 @@ }); }); + // Enable max attachmnets. + $('.attachments').each(function() { + enableMaxAttachments($(this)); + }); + + // 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 +471,9 @@ } attachment.find('.item-title').empty().append(thumbnail).append(title); } + + // Enable max attachmnets. + enableMaxAttachments($('.selecting-attachment').closest('.attachments')); }); $('#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 3670d94af..d6675b20a 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 = null) { 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 b85a85cdb..58c0229b5 100644 --- a/application/view/common/attachments-form.phtml +++ b/application/view/common/attachments-form.phtml @@ -18,7 +18,7 @@ $attachmentRowTemplate = ' ?>