Skip to content

Commit

Permalink
Merge pull request #2190 from omeka/max-block-attachments-option
Browse files Browse the repository at this point in the history
Max block attachments option
  • Loading branch information
zerocrates committed May 9, 2024
2 parents 0761ab9 + b58bd58 commit ebaf3b3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
33 changes: 33 additions & 0 deletions application/asset/js/site-page-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion application/src/View/Helper/BlockAttachmentsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}
2 changes: 1 addition & 1 deletion application/view/common/attachments-form.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $attachmentRowTemplate = '
?>
<div class="attachments-form<?php echo $this->itemOnly ? ' attachments-item-only' : ''; ?>" data-item-query="<?php echo $escape(json_encode($this->itemQuery)); ?>">
<a href="#" class="collapse"><h4><?php echo $translate('Attachments'); ?></h4></a>
<div class="attachments collapsible" data-template="<?php echo $escape($attachmentRowTemplate); ?>">
<div class="attachments collapsible" data-template="<?php echo $escape($attachmentRowTemplate); ?>" data-max-attachments="<?php echo $this->escapeHtml($maxAttachments); ?>">
<?php foreach ($attachments as $attachment): ?>
<?php
$itemId = null;
Expand Down

0 comments on commit ebaf3b3

Please sign in to comment.