Skip to content

Commit

Permalink
Refactor block group
Browse files Browse the repository at this point in the history
  • Loading branch information
jimsafley committed Dec 11, 2023
1 parent efafaac commit 0b02a4e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
4 changes: 1 addition & 3 deletions application/src/Site/BlockLayout/BlockGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function form(PhpRenderer $view, SiteRepresentation $site,
'info' => 'Number of blocks to include in this group. Groups may not overlap.', // @translate
])
->setAttribute('min', '1')
->setAttribute('class', 'block-group-span')
->setValue($block ? $block->dataValue('span') : '1');
$form->add($elementSpan);

Expand All @@ -34,9 +35,6 @@ public function form(PhpRenderer $view, SiteRepresentation $site,
'label' => 'Class', // @translate
'info' => 'Optional CSS class for this group.', // @translate
])
->setAttributes([
'class' => 'block-group-span',
])
->setValue($block ? $block->dataValue('class') : '');
$form->add($elementClass);

Expand Down
42 changes: 17 additions & 25 deletions application/src/View/Helper/PageLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,18 @@ public function render(SitePageRepresentation $page)
$this->eventManager->triggerEvent(new Event('page_layout.inline_styles', $page, $eventArgs));
$inlineStyles = $eventArgs['inline_styles'];

$gridColumns = (int) $page->layoutDataValue('grid_columns');
$gridColumnGap = (int) $page->layoutDataValue('grid_column_gap', 10);
$gridRowGap = (int) $page->layoutDataValue('grid_row_gap', 10);

// Prepare the page layout.
switch ($page->layout()) {
case 'grid':
$view->headLink()->appendStylesheet($view->assetUrl('css/page-grid.css', 'Omeka'));
$classes[] = 'page-layout-grid';
$inlineStyles[] = sprintf(
'grid-template-columns: repeat(%s, 1fr);',
(int) $page->layoutDataValue('grid_columns')
);
$inlineStyles[] = sprintf(
'column-gap: %spx;',
(int) $page->layoutDataValue('grid_column_gap', 10)
);
$inlineStyles[] = sprintf(
'row-gap: %spx;',
(int) $page->layoutDataValue('grid_row_gap', 10)
);
$inlineStyles[] = sprintf('grid-template-columns: repeat(%s, 1fr);', $gridColumns);
$inlineStyles[] = sprintf('column-gap: %spx;', $gridColumnGap);
$inlineStyles[] = sprintf('row-gap: %spx;', $gridRowGap);
break;
case '':
default:
Expand All @@ -58,6 +53,7 @@ public function render(SitePageRepresentation $page)
$view->escapeHtml(implode(' ', $inlineStyles))
);
$layouts = [];
$inBlockGroup = false;
foreach ($page->blocks() as $block) {
if (!array_key_exists($block->layout(), $layouts)) {
// Prepare render only once per block layout type.
Expand All @@ -66,24 +62,21 @@ public function render(SitePageRepresentation $page)
}
if ('blockGroup' === $block->layout()) {
// The blockGroup block gets special treatment.
if (isset($blockGroupSpan) && $blockGroupCurrentSpan < $blockGroupSpan) {
// Blocks may not overlap.
echo '</div>';
unset($blockGroupSpan, $blockGroupCurrentSpan);
if ($inBlockGroup) {
echo '</div>'; // Blocks may not overlap.
}
$inBlockGroup = true;
$blockGroupSpan = (int) $block->dataValue('span');
$blockGroupCurrentSpan = 0;
echo sprintf(
'<div class="block-group %s" style="display: grid; grid-template-columns: repeat(%s, 1fr); grid-column: span %s;">',
'<div class="block-group %1$s" style="display: grid; grid-template-columns: repeat(%2$s, 1fr); grid-column: span %2$s;">',
$view->escapeHtml($block->dataValue('class')),
$view->escapeHtml((int) $page->layoutDataValue('grid_columns')),
$view->escapeHtml((int) $page->layoutDataValue('grid_columns'))
$view->escapeHtml($gridColumns)
);
} else {
// Render each block according to page layout.
switch ($page->layout()) {
case 'grid':
$gridColumns = (int) $page->layoutDataValue('grid_columns');
$blockLayoutData = $block->layoutData();
$getValidPosition = fn ($columnPosition) => in_array($columnPosition, ['auto',...range(1, $gridColumns)]) ? $columnPosition : 'auto';
$getValidSpan = fn ($columnSpan) => in_array($columnSpan, range(1, $gridColumns)) ? $columnSpan : $gridColumns;
Expand All @@ -101,18 +94,17 @@ public function render(SitePageRepresentation $page)
}
}
// The blockGroup block gets special treatment.
if (isset($blockGroupSpan)) {
if ($inBlockGroup) {
if ($blockGroupCurrentSpan == $blockGroupSpan) {
echo '</div>';
unset($blockGroupSpan, $blockGroupCurrentSpan);
$inBlockGroup = false;
} else {
$blockGroupCurrentSpan++;
}
}
}
if (isset($blockGroupSpan)) {
// Close the blockGroup block if not already closed.
echo '</div>';
if ($inBlockGroup) {
echo '</div>'; // Close the blockGroup block if not already closed.
}
echo '</div>';
}
Expand Down
2 changes: 2 additions & 0 deletions application/view/common/block-layout.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ $layoutGridColumnSpan = $layoutData['grid_column_span'] ?? '12';
</span>
<?php endif; ?>
<ul class="actions">
<?php if ('blockGroup' !== $layout): ?>
<li><a href="#" class="o-icon-search preview-block-page-layout-grid" aria-label="<?php echo $translate('Preview layout'); ?>" title="<?php echo $translate('Preview layout'); ?>"></a></li>
<li><a href="#" class="o-icon-settings configure-block-layout-data" aria-label="<?php echo $translate('Configure layout'); ?>" title="<?php echo $translate('Configure layout'); ?>"></a></li>
<?php endif; ?>
<li><a href="#" class="o-icon-delete remove-value" aria-label="<?php echo $translate('Remove block'); ?>" title="<?php echo $translate('Remove block'); ?>"></a></li>
<li><a href="#" class="o-icon-undo restore-value inactive" aria-label="<?php echo $translate('Restore block'); ?>" title="<?php echo $translate('Restore block'); ?>"></a></li>
<li><a href="#" class="o-icon-expand collapse" aria-label="<?php echo $translate('Collapse'); ?>" title="<?php echo $translate('Collapse'); ?>"></a></li>
Expand Down

0 comments on commit 0b02a4e

Please sign in to comment.