You are here

GridStackBase.php in GridStack 8.2

File

src/Plugin/gridstack/engine/GridStackBase.php
View source
<?php

namespace Drupal\gridstack\Plugin\gridstack\engine;

use Drupal\Component\Utility\Html;
use Drupal\gridstack\GridStackEnginePluginBase;
use Drupal\gridstack\Entity\GridStack;

/**
 * Provides the base class for two-dimensional layout engines.
 */
abstract class GridStackBase extends GridStackEnginePluginBase {

  /**
   * The GridStack nodes.
   *
   * @var array
   */
  protected $nodes = [
    'x',
    'y',
    'width',
    'height',
    'region',
  ];

  /**
   * {@inheritdoc}
   */
  public function attach(array &$load, array $attach = []) {
    parent::attach($load, $attach);

    // Only load libraries if not destroyed, nor using Bootstrap/ Foundation.
    if ($this
      ->getSetting('_gridstack')) {
      $load['drupalSettings']['gridstack'] = GridStack::defaultSettings();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function build(array &$build, array &$element) {
    parent::build($build, $element);

    // Provides dummy item to fix layout aspect ratio.
    if ($this
      ->getSetting('_gridstack')) {
      $build['postscript']['dummy'] = $this
        ->buildDummyItem();
    }
  }

  /**
   * Returns the dummy box to measure cell height to fix aspect ratio.
   */
  protected function buildDummyItem() {
    $item['attributes']['class'][] = 'gridstack__sizer is-nixbox';
    $item['attributes']['data-gs-height'] = 1;
    $item['attributes']['data-gs-width'] = 1;
    $settings['use_inner'] = $settings['_dummy'] = TRUE;
    return $this
      ->buildItem(100, $settings, $item);
  }

  /**
   * {@inheritdoc}
   */
  protected function itemAttributes(array &$attributes, array &$settings) {
    parent::itemAttributes($attributes, $settings);
    $id = $this
      ->getSetting('delta', 0);
    $nid = $this
      ->getSetting('nested_delta', -1);
    $root = $this
      ->getSetting('_root', 'grids');
    $grids = $this
      ->getSetting('_grids') ?: $this
      ->getOptionset()
      ->getLastBreakpoint($root);

    // @todo nested grids for js-driven/ native Grid layouts, respects 0.
    if (isset($settings['nested_delta']) && $root == 'nested') {
      $node = isset($grids[$id][$nid]) ? $this
        ->getOptionset()
        ->getNode($grids[$id][$nid], FALSE) : [];
    }
    else {

      // The root element grids.
      $node = isset($grids[$id]) ? $this
        ->getOptionset()
        ->getNode($grids[$id], FALSE) : [];
    }
    if ($node) {
      $this
        ->nodeAttributes($attributes, $node);
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function nodeAttributes(array &$attributes, array $node) {
    $ns = $this
      ->getSetting('nameshort', 'gs');
    foreach ($this->nodes as $key) {
      if (isset($node[$key])) {
        if ($key == 'region' && $node[$key]) {
          $attributes['class'][] = 'box--' . Html::cleanCssIdentifier($node[$key]);
        }
        else {
          $attributes['data-' . $ns . '-' . $key] = (int) $node[$key];
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function containerAttributes(array &$attributes, array &$settings) {
    parent::containerAttributes($attributes, $settings);
    $attributes['class'][] = 'gridstack--js';

    // Responsive breakpoint related data-attributes helpers.
    $optionset = $this
      ->getOptionset();
    $attributes['data-gs-min-width'] = $this->minWidth;
    if ($optionset
      ->getSetting('noMargin', FALSE)) {
      $attributes['class'][] = 'is-gs-nomargin';
    }
    if ($vm = $optionset
      ->getSetting('verticalMargin')) {
      $attributes['data-gs-vm'] = $vm / 2;
    }

    // Do not proceed if gridstack is disabled.
    if ($this
      ->getSetting('ungridstack')) {
      $attributes['class'][] = 'ungridstack';
      return;
    }

    // Use the customized gridstack static without dnd if so configured.
    if ($this
      ->getSetting('gridstatic') && !$this
      ->getSetting('gridnative')) {
      $attributes['class'][] = 'gridstack--static';
    }

    // JS-enabled layouts, including native Grid.
    // Cannot rely on the library grid-stack-static, since not always there
    // such as when using native Grid which doesn't load GridStack library.
    if (!$this
      ->getSetting('_ipe')) {
      $attributes['class'][] = 'is-gs-packing';
    }
    $attributes['class'][] = 'is-gs-enabled';

    // Adds attributes for js-driven or native Grid layouts so to be responsive.
    // Supports dynamic like Isotope to have both native CSS and JS layouts.
    $attributes['data-gs-column'] = $optionset
      ->getLastColumn();
    $attributes['data-gs-columns'] = $optionset
      ->getJson('breakpoints');
    $attributes['data-gs-config'] = $optionset
      ->getJson('settings');
    $attributes['data-gs-data'] = $optionset
      ->getData();
  }

  /**
   * {@inheritdoc}
   */
  protected function itemContentAttributes(array &$attributes, array &$settings) {
    parent::itemContentAttributes($attributes, $settings);

    // Provides configurable content attributes via Layout Builder.
    $this
      ->attributes($attributes, $settings);
  }

}

Classes

Namesort descending Description
GridStackBase Provides the base class for two-dimensional layout engines.