c535897e65be30a4a0f7a17d2c9d28e807730dba
[yaffs-website] / web / modules / contrib / slick / src / Entity / Slick.php
1 <?php
2
3 namespace Drupal\slick\Entity;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Component\Utility\NestedArray;
7 use Drupal\Core\Config\Entity\ConfigEntityBase;
8
9 /**
10  * Defines the Slick configuration entity.
11  *
12  * @ConfigEntityType(
13  *   id = "slick",
14  *   label = @Translation("Slick optionset"),
15  *   list_path = "admin/config/media/slick",
16  *   config_prefix = "optionset",
17  *   entity_keys = {
18  *     "id" = "name",
19  *     "label" = "label",
20  *     "status" = "status",
21  *     "weight" = "weight",
22  *   },
23  *   config_export = {
24  *     "id",
25  *     "name",
26  *     "label",
27  *     "status",
28  *     "weight",
29  *     "group",
30  *     "skin",
31  *     "breakpoints",
32  *     "optimized",
33  *     "options",
34  *   }
35  * )
36  */
37 class Slick extends ConfigEntityBase implements SlickInterface {
38
39   /**
40    * The legacy CTools ID for the configurable optionset.
41    *
42    * @var string
43    */
44   protected $name;
45
46   /**
47    * The human-readable name for the optionset.
48    *
49    * @var string
50    */
51   protected $label;
52
53   /**
54    * The weight to re-arrange the order of slick optionsets.
55    *
56    * @var int
57    */
58   protected $weight = 0;
59
60   /**
61    * The optionset group for easy selections.
62    *
63    * @var string
64    */
65   protected $group = '';
66
67   /**
68    * The skin name for the optionset.
69    *
70    * @var string
71    */
72   protected $skin = '';
73
74   /**
75    * The number of breakpoints for the optionset.
76    *
77    * @var int
78    */
79   protected $breakpoints = 0;
80
81   /**
82    * The flag indicating to optimize the stored options by removing defaults.
83    *
84    * @var bool
85    */
86   protected $optimized = FALSE;
87
88   /**
89    * The plugin instance options.
90    *
91    * @var array
92    */
93   protected $options = [];
94
95   /**
96    * {@inheritdoc}
97    */
98   public function __construct(array $values, $entity_type = 'slick') {
99     parent::__construct($values, $entity_type);
100   }
101
102   /**
103    * Overrides Drupal\Core\Entity\Entity::id().
104    */
105   public function id() {
106     return $this->name;
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public function getSkin() {
113     return $this->skin;
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function getBreakpoints() {
120     return $this->breakpoints;
121   }
122
123   /**
124    * {@inheritdoc}
125    */
126   public function getGroup() {
127     return $this->group;
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function optimized() {
134     return $this->optimized;
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function getOptions($group = NULL, $property = NULL) {
141     if ($group) {
142       if (is_array($group)) {
143         return NestedArray::getValue($this->options, (array) $group);
144       }
145       elseif (isset($property) && isset($this->options[$group])) {
146         return isset($this->options[$group][$property]) ? $this->options[$group][$property] : NULL;
147       }
148       return $this->options[$group];
149     }
150
151     return $this->options;
152   }
153
154   /**
155    * {@inheritdoc}
156    */
157   public function getSettings() {
158     // With the Optimized options, all defaults are cleaned out, merge em.
159     return isset($this->options['settings']) ? array_merge(self::defaultSettings(), $this->options['settings']) : self::defaultSettings();
160   }
161
162   /**
163    * {@inheritdoc}
164    */
165   public function setSettings(array $settings = []) {
166     $this->options['settings'] = $settings;
167     return $this;
168   }
169
170   /**
171    * {@inheritdoc}
172    */
173   public function getSetting($setting_name) {
174     return isset($this->getSettings()[$setting_name]) ? $this->getSettings()[$setting_name] : NULL;
175   }
176
177   /**
178    * {@inheritdoc}
179    */
180   public static function defaultSettings($group = 'settings') {
181     return self::load('default')->options[$group];
182   }
183
184   /**
185    * Overrides \Drupal\Core\Entity\Entity::create().
186    */
187   public static function create(array $values = []) {
188     $optionset = parent::create($values);
189
190     $optionset->setSettings($optionset->getSettings());
191     return $optionset;
192   }
193
194   /**
195    * Returns the Slick responsive settings.
196    */
197   public function getResponsiveOptions() {
198     if (empty($this->breakpoints)) {
199       return FALSE;
200     }
201     $options = [];
202     if (isset($this->options['responsives']['responsive'])) {
203       $responsives = $this->options['responsives'];
204       if ($responsives['responsive']) {
205         foreach ($responsives['responsive'] as $delta => $responsive) {
206           if (empty($responsives['responsive'][$delta]['breakpoint'])) {
207             unset($responsives['responsive'][$delta]);
208           }
209           if (isset($responsives['responsive'][$delta])) {
210             $options[$delta] = $responsive;
211           }
212         }
213       }
214     }
215     return $options;
216   }
217
218   /**
219    * Sets the Slick responsive settings.
220    */
221   public function setResponsiveSettings($settings, $delta = 0) {
222     $this->options['responsives']['responsive'][$delta]['settings'] = $settings;
223     return $this;
224   }
225
226   /**
227    * Strip out options containing default values so to have real clean JSON.
228    */
229   public function removeDefaultValues(array $js) {
230     $config   = [];
231     $defaults = self::defaultSettings();
232
233     // Remove wasted dependent options if disabled, empty or not.
234     $this->removeWastedDependentOptions($js);
235     $config = array_diff_assoc($js, $defaults);
236
237     // Remove empty lazyLoad, or left to default ondemand, to avoid JS error.
238     if (empty($config['lazyLoad'])) {
239       unset($config['lazyLoad']);
240     }
241
242     // Do not pass arrows HTML to JSON object as some are enforced.
243     $excludes = [
244       'downArrow',
245       'downArrowTarget',
246       'downArrowOffset',
247       'prevArrow',
248       'nextArrow',
249     ];
250     foreach ($excludes as $key) {
251       unset($config[$key]);
252     }
253
254     // Clean up responsive options if similar to defaults.
255     if ($responsives = $this->getResponsiveOptions()) {
256       $cleaned = [];
257       foreach ($responsives as $key => $responsive) {
258         $cleaned[$key]['breakpoint'] = $responsives[$key]['breakpoint'];
259
260         // Destroy responsive slick if so configured.
261         if ($responsives[$key]['unslick']) {
262           $cleaned[$key]['settings'] = 'unslick';
263           unset($responsives[$key]['unslick']);
264         }
265         else {
266           // Remove wasted dependent options if disabled, empty or not.
267           $this->removeWastedDependentOptions($responsives[$key]['settings']);
268           $cleaned[$key]['settings'] = array_diff_assoc($responsives[$key]['settings'], $defaults);
269         }
270       }
271       $config['responsive'] = $cleaned;
272     }
273     return $config;
274   }
275
276   /**
277    * Removes wasted dependent options, even if not empty.
278    */
279   public function removeWastedDependentOptions(array &$js) {
280     foreach (self::getDependentOptions() as $key => $option) {
281       if (isset($js[$key]) && empty($js[$key])) {
282         foreach ($option as $dependent) {
283           unset($js[$dependent]);
284         }
285       }
286     }
287
288     if (!empty($js['useCSS']) && !empty($js['cssEaseBezier'])) {
289       $js['cssEase'] = $js['cssEaseBezier'];
290     }
291     unset($js['cssEaseOverride'], $js['cssEaseBezier']);
292   }
293
294   /**
295    * Defines the dependent options.
296    */
297   public static function getDependentOptions() {
298     $down_arrow = ['downArrowTarget', 'downArrowOffset'];
299     return [
300       'arrows'     => ['prevArrow', 'nextArrow', 'downArrow'] + $down_arrow,
301       'downArrow'  => $down_arrow,
302       'autoplay'   => ['pauseOnHover', 'pauseOnDotsHover', 'autoplaySpeed'],
303       'centerMode' => ['centerPadding'],
304       'dots'       => ['dotsClass', 'appendDots'],
305       'swipe'      => ['swipeToSlide'],
306       'useCSS'     => ['cssEase', 'cssEaseBezier', 'cssEaseOverride'],
307       'vertical'   => ['verticalSwiping'],
308     ];
309   }
310
311   /**
312    * Returns the trusted HTML ID of a single slick instance.
313    *
314    * @todo: Consider Blazy::getHtmlId() instead.
315    */
316   public static function getHtmlId($string = 'slick', $id = '') {
317     $slick_id = &drupal_static('slick_id', 0);
318
319     // Do not use dynamic Html::getUniqueId, otherwise broken asnavfors.
320     return empty($id) ? Html::getId($string . '-' . ++$slick_id) : $id;
321   }
322
323   /**
324    * Returns HTML or layout related settings to shut up notices.
325    */
326   public static function htmlSettings() {
327     return [
328       'cache'             => 0,
329       'current_view_mode' => '',
330       'display'           => 'main',
331       'grid'              => 0,
332       'id'                => '',
333       'nav'               => FALSE,
334       'navpos'            => FALSE,
335       'media_switch'      => '',
336       'optionset'         => 'default',
337       'ratio'             => '',
338       'skin'              => '',
339       'unslick'           => FALSE,
340       'vanilla'           => FALSE,
341       'vertical'          => FALSE,
342       'vertical_tn'       => FALSE,
343       'view_name'         => '',
344     ];
345   }
346
347   /**
348    * Defines JS options required by theme_slick(), used with optimized option.
349    */
350   public static function jsSettings() {
351     return [
352       'asNavFor'        => '',
353       'downArrowTarget' => '',
354       'downArrowOffset' => '',
355       'lazyLoad'        => 'ondemand',
356       'prevArrow'       => '<button type="button" data-role="none" class="slick-prev" aria-label="Previous" tabindex="0" role="button">Previous</button>',
357       'nextArrow'       => '<button type="button" data-role="none" class="slick-next" aria-label="Next" tabindex="0" role="button">Next</button>',
358       'rows'            => 1,
359       'slidesPerRow'    => 1,
360       'slide'           => '',
361       'slidesToShow'    => 1,
362     ];
363   }
364
365 }