View source
<?php
namespace Drupal\gridstack;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Render\Element;
use Drupal\blazy\Blazy;
use Drupal\blazy\BlazyManagerBase;
use Drupal\gridstack\Entity\GridStack;
use Symfony\Component\DependencyInjection\ContainerInterface;
class GridStackManager extends BlazyManagerBase implements GridStackManagerInterface {
protected $engine;
protected $engineId;
protected $optionset;
protected $engineManager;
protected $skinManager;
protected $stylizer;
protected $mergedClasses;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance
->setEngineManager($container
->get('gridstack.engine_manager'));
$instance
->setSkinManager($container
->get('gridstack.skin_manager'));
$instance
->setStylizer($container
->get('gridstack.stylizer'));
return $instance;
}
public function engineManager() {
return $this->engineManager;
}
public function setEngineManager(GridStackEngineManagerInterface $engine_manager) {
$this->engineManager = $engine_manager;
return $this;
}
public function skinManager() {
return $this->skinManager;
}
public function setSkinManager(GridStackSkinManagerInterface $skin_manager) {
$this->skinManager = $skin_manager;
return $this;
}
public function stylizer() {
return $this->stylizer;
}
public function setStylizer(GridStackStylizerInterface $stylizer) {
$this->stylizer = $stylizer;
return $this;
}
public function getEngineId() {
if (!isset($this->engineId)) {
$this->engineId = 'gridstack_js';
}
return $this->engineId;
}
protected function setEngineId($engine_id) {
$this->engineId = $engine_id;
return $this;
}
public function getEngine(array $settings, $engine = NULL) {
if (!isset($this->engine) || $engine) {
if (empty($engine)) {
$engine = empty($settings['_engine']) ? $this
->getEngineId() : $settings['_engine'];
}
$this->engine = $this->engineManager
->load($engine, $settings);
}
return $this->engine;
}
protected function setEngine(array $settings) {
$this->engine = $this->engineManager
->load($settings['_engine'], $settings);
return $this;
}
public static function trustedCallbacks() {
return [
'preRenderGridStack',
];
}
public function attach(array $attach = []) {
$load = parent::attach($attach);
if (isset($this->engineManager)) {
$this->engineManager
->attach($load, $attach);
$this->skinManager
->attach($load, $attach);
$this->stylizer
->attach($load, $attach);
}
$this->moduleHandler
->alter('gridstack_attach', $load, $attach);
return $load;
}
public function getMergedClasses($flatten = FALSE) {
if (!isset($this->mergedClasses[$flatten])) {
$classes = $this->engineManager
->getClassOptions();
$this->mergedClasses[$flatten] = $this->stylizer
->getMergedClasses($flatten, $classes);
}
return $this->mergedClasses[$flatten];
}
protected function getGridStackSettings() {
return array_intersect_key($this
->configLoad('', 'gridstack.settings'), GridStackDefault::cleanUiSettings());
}
public function prepareSettings(array &$settings) {
$settings += array_merge(GridStackDefault::htmlSettings(), $this
->getGridStackSettings());
$settings['column'] = $this->optionset
->getSetting('column', 12);
$settings['use_nested'] = $settings['use_framework'] = !empty($settings['framework']) && $this->optionset
->getOption('use_framework');
$settings['gridnative'] = !$settings['use_framework'] && !empty($settings['gridnative']);
$settings['_gridstack'] = TRUE;
if ($settings['use_framework']) {
$settings['background'] = $settings['_gridstack'] = $settings['use_js'] = FALSE;
$settings['_engine'] = $settings['framework'];
}
else {
$settings['_gridstack'] = empty($settings['ungridstack']);
$fallback = $settings['gridnative'] ? 'gridstack_native' : 'gridstack_js';
$settings['_engine'] = empty($settings['_engine']) ? $fallback : $settings['_engine'];
}
$settings['id'] = Blazy::getHtmlId('gridstack-' . $settings['optionset'] . '-' . $settings['_engine'], $settings['id']);
$this
->setEngineId($settings['_engine']);
}
protected function initEngine(array &$build, array &$element) {
$settings =& $build['settings'];
$this
->setEngine($settings);
$this->engine
->build($build, $element);
}
public function prepareAttributes(array &$build) {
return [];
}
protected function prepareBuild(array &$build, array &$element) {
$id1 = 'data-layout-update-url';
$id2 = 'data-layout-builder-highlight-id';
$settings =& $build['settings'];
$settings['_lb'] = isset($element['#attributes']) && (isset($element['#attributes'][$id1]) || isset($element['#attributes'][$id2]));
$settings['_lbux'] = $settings['_lb'] && $this->moduleHandler
->moduleExists('lb_ux');
$settings['_panels'] = isset($element['#prefix']) && strpos($element['#prefix'], 'panels-ipe-content') !== FALSE;
$settings['_ipe'] = $settings['_lb'] || $settings['_lbux'] || $settings['_panels'];
if (!empty($settings['check_blazy']) && !empty($build['items'][0])) {
$this
->isBlazy($settings, $build['items'][0]);
}
if (empty($build['optionset'])) {
$build['optionset'] = GridStack::loadWithFallback($settings['optionset']);
}
$this->optionset = $build['optionset'];
$this
->prepareSettings($settings);
$this
->initEngine($build, $element);
}
public function build(array $build = []) {
foreach ([
'attached',
'layout',
'variant',
] + GridStackDefault::themeProperties() as $key) {
$build[$key] = isset($build[$key]) ? $build[$key] : [];
}
$gridstack = [
'#theme' => 'gridstack',
'#build' => $build,
'#pre_render' => [
[
$this,
'preRenderGridStack',
],
],
'items' => [],
'#layout' => $build['layout'],
];
$this->moduleHandler
->alter('gridstack_build', $gridstack, $build['settings']);
return empty($build['items']) ? [] : $gridstack;
}
public function preRenderGridStack($element) {
$build = $element['#build'];
unset($element['#build']);
$this
->prepareBuild($build, $element);
$settings =& $build['settings'];
$cache = $this
->getCacheMetadata($build);
$attributes = $this
->prepareAttributes($build);
$attachments = $this
->attach($settings);
$attachments = empty($build['attached']) ? $attachments : NestedArray::mergeDeep($build['attached'], $attachments);
$element['#optionset'] = $this->optionset;
$element['#settings'] = empty($element['#settings']) ? $settings : NestedArray::mergeDeep($element['#settings'], $settings);
$element['#postscript'] = empty($element['#postscript']) ? $build['postscript'] : NestedArray::mergeDeep($element['#postscript'], $build['postscript']);
$element['#attributes'] = empty($element['#attributes']) ? $attributes : NestedArray::mergeDeep($element['#attributes'], $attributes);
$element['#attached'] = empty($element['#attached']) ? $attachments : NestedArray::mergeDeep($element['#attached'], $attachments);
$element['#cache'] = empty($element['#cache']) ? $cache : NestedArray::mergeDeep($element['#cache'], $cache);
foreach (Element::children($element) as $child) {
unset($element[$child]);
}
unset($build);
return $element;
}
}