View source
<?php
namespace Drupal\slick\Entity;
use Drupal\blazy\Blazy;
class Slick extends SlickBase implements SlickInterface {
protected $group = '';
protected $skin = '';
protected $breakpoints = 0;
protected $optimized = FALSE;
public function getSkin() {
return $this->skin;
}
public function getBreakpoints() {
return $this->breakpoints;
}
public function getGroup() {
return $this->group;
}
public function optimized() {
return $this->optimized;
}
public function getResponsiveOptions() {
if (empty($this->breakpoints)) {
return FALSE;
}
$options = [];
if (isset($this->options['responsives']['responsive'])) {
$responsives = $this->options['responsives'];
if ($responsives['responsive']) {
foreach ($responsives['responsive'] as $delta => $responsive) {
if (empty($responsives['responsive'][$delta]['breakpoint'])) {
unset($responsives['responsive'][$delta]);
}
if (isset($responsives['responsive'][$delta])) {
$options[$delta] = $responsive;
}
}
}
}
return $options;
}
public function setResponsiveSettings($values, $delta = 0, $key = 'settings') {
$this->options['responsives']['responsive'][$delta][$key] = $values;
return $this;
}
public function removeDefaultValues(array $js) {
$config = [];
$defaults = self::defaultSettings();
if (!$this->optimized) {
$this
->removeWastedDependentOptions($js);
}
$config = array_diff_assoc($js, $defaults);
if (empty($config['lazyLoad'])) {
unset($config['lazyLoad']);
}
$excludes = [
'downArrow',
'downArrowTarget',
'downArrowOffset',
'prevArrow',
'nextArrow',
];
foreach ($excludes as $key) {
unset($config[$key]);
}
if ($responsives = $this
->getResponsiveOptions()) {
$cleaned = [];
foreach ($responsives as $key => $responsive) {
$cleaned[$key]['breakpoint'] = $responsives[$key]['breakpoint'];
if (!empty($responsives[$key]['unslick'])) {
$cleaned[$key]['settings'] = 'unslick';
unset($responsives[$key]['unslick']);
}
else {
if (!$this->optimized) {
$this
->removeWastedDependentOptions($responsives[$key]['settings']);
}
$cleaned[$key]['settings'] = array_diff_assoc($responsives[$key]['settings'], $defaults);
}
}
$config['responsive'] = $cleaned;
}
return $config;
}
public function removeWastedDependentOptions(array &$js) {
foreach (self::getDependentOptions() as $key => $option) {
if (isset($js[$key]) && empty($js[$key])) {
foreach ($option as $dependent) {
unset($js[$dependent]);
}
}
}
if (!empty($js['useCSS']) && !empty($js['cssEaseBezier'])) {
$js['cssEase'] = $js['cssEaseBezier'];
}
unset($js['cssEaseOverride'], $js['cssEaseBezier']);
}
public static function getDependentOptions() {
$down_arrow = [
'downArrowTarget',
'downArrowOffset',
];
return [
'arrows' => [
'arrowsPlacement',
'prevArrow',
'nextArrow',
'downArrow',
] + $down_arrow,
'downArrow' => $down_arrow,
'autoplay' => [
'pauseOnHover',
'pauseOnDotsHover',
'pauseOnFocus',
'autoplaySpeed',
'useAutoplayToggleButton',
'pauseIcon',
'playIcon',
],
'centerMode' => [
'centerPadding',
],
'dots' => [
'dotsClass',
'appendDots',
],
'swipe' => [
'swipeToSlide',
],
'useCSS' => [
'cssEase',
'cssEaseBezier',
'cssEaseOverride',
],
'vertical' => [
'verticalSwiping',
],
];
}
public function whichLazy(array &$settings) {
$lazy = $this
->getSetting('lazyLoad');
$settings['blazy'] = $lazy == 'blazy' || !empty($settings['blazy']);
$settings['lazy'] = $settings['blazy'] ? 'blazy' : $lazy;
if (empty($settings['blazy'])) {
$settings['lazy_class'] = $settings['lazy_attribute'] = 'lazy';
}
$settings['lazy'] = empty($settings['is_preview']) ? $settings['lazy'] : '';
$settings['_lazy'] = TRUE;
}
public static function getHtmlId($string = 'slick', $id = '') {
return Blazy::getHtmlId($string, $id);
}
}