Skip to content

Commit

Permalink
Move duplicate code to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
jimsafley committed May 6, 2024
1 parent 539e256 commit b58bd58
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 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,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.
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b58bd58

Please sign in to comment.