You are here

public function GridStackManager::setBoxAttributes in GridStack 8

Provides box attributes.

Available attributes:

  • Base: x, y, width, height.
  • Extra: autoPosition, minWidth, maxWidth, minHeight, maxHeight, id.

Parameters

array $settings: The settings being modified.

string $current: The current box identifier: grids, or nested.

object $optionset: The \Drupal\gridstack\Entity\GridStack GridStack instance.

Return value

array The array of attributes for each box, either main, or nested boxes.

1 call to GridStackManager::setBoxAttributes()
GridStackManager::buildItems in src/GridStackManager.php
Modifies GridStack boxes to support nested grids for Bootstrap/ Foundation.

File

src/GridStackManager.php, line 120

Class

GridStackManager
Implements GridStackManagerInterface.

Namespace

Drupal\gridstack

Code

public function setBoxAttributes(array &$settings = [], $current = 'grids', $optionset = NULL) {
  $attributes = [];
  $breakpoint = isset($settings['breakpoint']) ? $settings['breakpoint'] : 'lg';
  $breakpoints = isset($settings['breakpoints']) ? $settings['breakpoints'] : [];
  $id = isset($settings['delta']) ? $settings['delta'] : 0;
  $nid = isset($settings['nested_delta']) ? $settings['nested_delta'] : NULL;
  $use_js = empty($settings['use_framework']) && !empty($settings['root']) || !empty($settings['_admin']);

  // Provides GridStack JS grid attributes.
  if ($use_js) {
    $end_grids = $optionset
      ->getEndBreakpointGrids($current);
    $grids = isset($breakpoints[$breakpoint]) && isset($breakpoints[$breakpoint][$current]) ? $breakpoints[$breakpoint][$current] : $end_grids;
    if (empty($grids)) {
      return $attributes;
    }

    // Nested grids.
    if (isset($settings['nested_delta']) && $current == 'nested') {
      foreach ([
        'x',
        'y',
        'width',
        'height',
      ] as $key) {
        if (!isset($grids[$id][$nid])) {
          continue;
        }
        $value = isset($grids[$id][$nid][$key]) ? $grids[$id][$nid][$key] : 0;
        $attributes['data-gs-' . $key] = $value;
      }
    }
    else {

      // The root element grids.
      foreach ([
        'x',
        'y',
        'width',
        'height',
      ] as $key) {
        $value = isset($grids[$id][$key]) ? $grids[$id][$key] : 0;
        $attributes['data-gs-' . $key] = $value;
      }
    }
    return $attributes;
  }

  // Provides static Bootstrap/ Foundation grid attributes.
  // When this is hit, GridStack JS and CSS assets are not loaded.
  $framework = $settings['framework'];
  $points = [
    'xs' => 'xsmall',
    'sm' => 'small',
    'md' => 'medium',
    'lg' => 'large',
    'xl' => 'xlarge',
  ];
  $regions = isset($settings['regions']) ? $settings['regions'] : [];
  $region = $current == 'nested' ? 'gridstack_' . $id . '_' . $nid : 'gridstack_' . $id;
  if (!empty($regions[$region])) {
    if (isset($regions[$region]['attributes']) && !empty($regions[$region]['attributes'])) {
      $attributes = $optionset::parseAttributes($regions[$region]['attributes']);
      unset($settings['regions'][$region]['attributes']);
    }

    // @todo: Remove BC $old_classes.
    $old_classes = !empty($regions[$region]['classes']) ? $regions[$region]['classes'] : '';
    $new_classes = !empty($regions[$region]['wrapper_classes']) ? $regions[$region]['wrapper_classes'] : $old_classes;
    if (!empty($new_classes)) {
      $classes = explode(' ', $new_classes);
      foreach ($classes as $class) {
        $attributes['class'][] = trim($class);
      }
      unset($settings['regions'][$region]['wrapper_classes']);
    }
  }

  // Bootstrap 4 uses flexbox with `col` class, and has `xl` breakpoint.
  // @todo: Make it generic for other frameworks, or at least alterable.
  if ($framework == 'bootstrap') {
    $attributes['class'][] = 'col';
  }
  elseif ($framework == 'foundation') {
    unset($points['xs'], $points['xl']);
  }
  $unique = $optionset::optimizeGridWidths($settings, $current);
  foreach ($points as $point => $label) {
    if (!isset($unique[$point])) {
      continue;
    }
    $prefix = $suffix = '';
    if (strpos($framework, 'bootstrap') !== FALSE) {

      // Specific to XS: Bootstrap 3: col-xs-*, Bootstrap 4: col-*.
      $prefix = 'col-' . $point . '-';
      if ($framework == 'bootstrap' && $point == 'xs') {
        $prefix = 'col-';
      }
    }
    elseif ($framework == 'foundation') {
      $prefix = $label . '-';
      $suffix = ' columns';
    }
    $attributes['class'][] = $prefix . $unique[$point] . $suffix;
  }
  return $attributes;
}