gridstack-8.x-2.5/src/GridStackSkinManager.php

src/GridStackSkinManager.php
<?php

namespace Drupal\gridstack;

use Drupal\Component\Plugin\Mapper\MapperInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\gridstack\Entity\GridStack;

/**
 * Implements GridStackSkinManagerInterface.
 */
class GridStackSkinManager extends DefaultPluginManager implements GridStackSkinManagerInterface, MapperInterface {

  use StringTranslationTrait;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * Static cache for the skin definition.
   *
   * @var array
   */
  protected $skinDefinition;

  /**
   * Static cache for the skin options.
   *
   * @var array
   */
  protected $skinOptions;

  /**
   * The library info definition.
   *
   * @var array
   */
  protected $libraryInfoBuild;

  /**
   * {@inheritdoc}
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config) {
    parent::__construct('Plugin/gridstack', $namespaces, $module_handler, GridStackSkinPluginInterface::class, 'Drupal\gridstack\Annotation\GridStackSkin');

    $this->config = $config;
    $this->alterInfo('gridstack_skin_info');
    $this->setCacheBackend($cache_backend, 'gridstack_skin_plugins');
  }

  /**
   * Returns gridstack config shortcut.
   */
  public function config($key = '', $settings = 'gridstack.settings') {
    return $this->config->get($settings)->get($key);
  }

  /**
   * Returns cache backend service.
   */
  public function getCache() {
    return $this->cacheBackend;
  }

  /**
   * {@inheritdoc}
   */
  public function load($plugin_id) {
    return $this->createInstance($plugin_id);
  }

  /**
   * {@inheritdoc}
   */
  public function loadMultiple() {
    $skins = [];
    foreach ($this->getDefinitions() as $definition) {
      array_push($skins, $this->createInstance($definition['id']));
    }
    return $skins;
  }

  /**
   * Returns gridstack skins registered via GridStackSkin plugin or defaults.
   */
  public function getSkins() {
    if (!isset($this->skinDefinition)) {
      $cid = 'gridstack_skins_data';

      if ($cache = $this->cacheBackend->get($cid)) {
        $this->skinDefinition = $cache->data;
      }
      else {
        $skins = [];
        foreach ($this->loadMultiple() as $skin) {
          $skins = NestedArray::mergeDeep($skins, $skin->skins());
        }

        $count = count($skins);
        $tags = Cache::buildTags($cid, ['count:' . $count]);
        $this->cacheBackend->set($cid, $skins, Cache::PERMANENT, $tags);

        $this->skinDefinition = $skins;
      }
    }
    return $this->skinDefinition;
  }

  /**
   * Returns available skins for select options.
   */
  public function getSkinOptions() {
    if (!isset($this->skinOptions)) {
      $skins = [];
      foreach ($this->getSkins() as $skin => $properties) {
        $skins[$skin] = isset($properties['name']) ? strip_tags($properties['name']) : $skin;
      }
      $this->skinOptions = $skins;
    }
    return $this->skinOptions;
  }

  /**
   * Provides gridstack skins and libraries.
   */
  public function attach(array &$load, array $attach = []) {
    if (!empty($attach['debug'])) {
      $load['library'][] = 'gridstack/debug';
    }

    // Admin assets.
    if (!empty($attach['_access_ipe'])) {
      $load['library'][] = 'gridstack/admin_layout';
    }

    // Only load libraries if not destroyed.
    if (empty($attach['ungridstack'])) {
      // If not using Bootstrap, nor Foundation.
      if (!empty($attach['use_js'])) {

        // Do not load GridStack assets if using native CSS Grid.
        if (empty($attach['gridnative'])) {
          $load['library'][] = 'gridstack/load';

          if (!empty($attach['column']) && $attach['column'] < 12) {
            $load['library'][] = 'gridstack/gridstack.' . $attach['column'];
          }

          // Breakpoints: xs sm md lg xl requires separate CSS files.
          if (!empty($attach['breakpoints'])) {
            foreach ($attach['breakpoints'] as $breakpoint) {
              if (!empty($breakpoint['column']) && $breakpoint['column'] < 12) {
                $load['library'][] = 'gridstack/gridstack.' . $breakpoint['column'];
              }
            }
          }
        }
        else {
          // Load native browser CSS/ JS assets if so configured.
          $load['library'][] = 'gridstack/native';
        }

        // Required by CSS Grid and GridStack JS for configurable options.
        $load['drupalSettings']['gridstack'] = GridStack::defaultSettings();
      }
    }
  }

  /**
   * Provides skins only if required.
   */
  public function attachSkin(array &$load, $attach = []) {
    // The CSS framework grid library for admin pages such as layout builder.
    if (!empty($attach['library'])) {
      $library = $attach['library'];
      if (strpos($library, ',') !== FALSE) {
        $items = array_map('trim', explode(',', $library));
        foreach ($items as $item) {
          $load['library'][] = $item;
        }
      }
      else {
        $load['library'][] = $library;
      }
    }

    // Skins may be available for JS, or CSS layouts, or even ungridstack.
    if (isset($attach['skin']) && $skin = $attach['skin']) {
      $skins = $this->getSkins();
      $provider = isset($skins[$skin]['provider']) ? $skins[$skin]['provider'] : 'gridstack';
      $load['library'][] = 'gridstack/' . $provider . '.' . $skin;
    }
  }

  /**
   * Implements hook_library_info_build().
   */
  public function libraryInfoBuild() {
    if (!isset($this->libraryInfoBuild)) {
      $libraries = [];
      foreach ($this->getSkins() as $key => $skin) {
        $provider = isset($skin['provider']) ? $skin['provider'] : 'gridstack';
        $id = $provider . '.' . $key;

        foreach (['css', 'js', 'dependencies'] as $property) {
          if (isset($skin[$property]) && is_array($skin[$property])) {
            $libraries[$id][$property] = $skin[$property];
          }
        }
        $libraries[$id]['dependencies'][] = 'gridstack/skin';
      }

      foreach (range(1, 12) as $key) {
        $libraries['gridstack.' . $key] = [
          'css' => [
            'layout' => ['css/layout/grid-stack-' . $key . '.css' => []],
          ],
          'dependencies' => ['gridstack/library'],
        ];
      }

      $this->libraryInfoBuild = $libraries;
    }

    return $this->libraryInfoBuild;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc