Added missing modules, including some as submodules.
[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    * The slick HTML ID.
97    *
98    * @var int
99    */
100   private static $slickId;
101
102   /**
103    * {@inheritdoc}
104    */
105   public function __construct(array $values, $entity_type = 'slick') {
106     parent::__construct($values, $entity_type);
107   }
108
109   /**
110    * Overrides Drupal\Core\Entity\Entity::id().
111    */
112   public function id() {
113     return $this->name;
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function getSkin() {
120     return $this->skin;
121   }
122
123   /**
124    * {@inheritdoc}
125    */
126   public function getBreakpoints() {
127     return $this->breakpoints;
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function getGroup() {
134     return $this->group;
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function optimized() {
141     return $this->optimized;
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   public function getOptions($group = NULL, $property = NULL) {
148     if ($group) {
149       if (is_array($group)) {
150         return NestedArray::getValue($this->options, (array) $group);
151       }
152       elseif (isset($property) && isset($this->options[$group])) {
153         return isset($this->options[$group][$property]) ? $this->options[$group][$property] : NULL;
154       }
155       return $this->options[$group];
156     }
157
158     return $this->options;
159   }
160
161   /**
162    * {@inheritdoc}
163    */
164   public function getSettings() {
165     // With the Optimized options, all defaults are cleaned out, merge em.
166     return isset($this->options['settings']) ? array_merge(self::defaultSettings(), $this->options['settings']) : self::defaultSettings();
167   }
168
169   /**
170    * {@inheritdoc}
171    */
172   public function setSettings(array $settings = []) {
173     $this->options['settings'] = $settings;
174     return $this;
175   }
176
177   /**
178    * {@inheritdoc}
179    */
180   public function getSetting($name) {
181     return isset($this->getSettings()[$name]) ? $this->getSettings()[$name] : NULL;
182   }
183
184   /**
185    * {@inheritdoc}
186    */
187   public function setSetting($name, $value) {
188     $this->options['settings'][$name] = $value;
189     return $this;
190   }
191
192   /**
193    * {@inheritdoc}
194    */
195   public static function defaultSettings($group = 'settings') {
196     return self::load('default')->options[$group];
197   }
198
199   /**
200    * Returns the Slick responsive settings.
201    *
202    * @return array
203    *   The responsive options.
204    */
205   public function getResponsiveOptions() {
206     if (empty($this->breakpoints)) {
207       return FALSE;
208     }
209     $options = [];
210     if (isset($this->options['responsives']['responsive'])) {
211       $responsives = $this->options['responsives'];
212       if ($responsives['responsive']) {
213         foreach ($responsives['responsive'] as $delta => $responsive) {
214           if (empty($responsives['responsive'][$delta]['breakpoint'])) {
215             unset($responsives['responsive'][$delta]);
216           }
217           if (isset($responsives['responsive'][$delta])) {
218             $options[$delta] = $responsive;
219           }
220         }
221       }
222     }
223     return $options;
224   }
225
226   /**
227    * Sets the Slick responsive settings.
228    *
229    * @return $this
230    *   The class instance that this method is called on.
231    */
232   public function setResponsiveSettings($values, $delta = 0, $key = 'settings') {
233     $this->options['responsives']['responsive'][$delta][$key] = $values;
234     return $this;
235   }
236
237   /**
238    * Strip out options containing default values so to have real clean JSON.
239    *
240    * @return array
241    *   The cleaned out settings.
242    */
243   public function removeDefaultValues(array $js) {
244     $config   = [];
245     $defaults = self::defaultSettings();
246
247     // Remove wasted dependent options if disabled, empty or not.
248     $this->removeWastedDependentOptions($js);
249     $config = array_diff_assoc($js, $defaults);
250
251     // Remove empty lazyLoad, or left to default ondemand, to avoid JS error.
252     if (empty($config['lazyLoad'])) {
253       unset($config['lazyLoad']);
254     }
255
256     // Do not pass arrows HTML to JSON object as some are enforced.
257     $excludes = [
258       'downArrow',
259       'downArrowTarget',
260       'downArrowOffset',
261       'prevArrow',
262       'nextArrow',
263     ];
264     foreach ($excludes as $key) {
265       unset($config[$key]);
266     }
267
268     // Clean up responsive options if similar to defaults.
269     if ($responsives = $this->getResponsiveOptions()) {
270       $cleaned = [];
271       foreach ($responsives as $key => $responsive) {
272         $cleaned[$key]['breakpoint'] = $responsives[$key]['breakpoint'];
273
274         // Destroy responsive slick if so configured.
275         if (!empty($responsives[$key]['unslick'])) {
276           $cleaned[$key]['settings'] = 'unslick';
277           unset($responsives[$key]['unslick']);
278         }
279         else {
280           // Remove wasted dependent options if disabled, empty or not.
281           $this->removeWastedDependentOptions($responsives[$key]['settings']);
282           $cleaned[$key]['settings'] = array_diff_assoc($responsives[$key]['settings'], $defaults);
283         }
284       }
285       $config['responsive'] = $cleaned;
286     }
287     return $config;
288   }
289
290   /**
291    * Removes wasted dependent options, even if not empty.
292    */
293   public function removeWastedDependentOptions(array &$js) {
294     foreach (self::getDependentOptions() as $key => $option) {
295       if (isset($js[$key]) && empty($js[$key])) {
296         foreach ($option as $dependent) {
297           unset($js[$dependent]);
298         }
299       }
300     }
301
302     if (!empty($js['useCSS']) && !empty($js['cssEaseBezier'])) {
303       $js['cssEase'] = $js['cssEaseBezier'];
304     }
305     unset($js['cssEaseOverride'], $js['cssEaseBezier']);
306   }
307
308   /**
309    * Defines the dependent options.
310    *
311    * @return array
312    *   The dependent options.
313    */
314   public static function getDependentOptions() {
315     $down_arrow = ['downArrowTarget', 'downArrowOffset'];
316     return [
317       'arrows'     => ['prevArrow', 'nextArrow', 'downArrow'] + $down_arrow,
318       'downArrow'  => $down_arrow,
319       'autoplay'   => ['pauseOnHover', 'pauseOnDotsHover', 'autoplaySpeed'],
320       'centerMode' => ['centerPadding'],
321       'dots'       => ['dotsClass', 'appendDots'],
322       'swipe'      => ['swipeToSlide'],
323       'useCSS'     => ['cssEase', 'cssEaseBezier', 'cssEaseOverride'],
324       'vertical'   => ['verticalSwiping'],
325     ];
326   }
327
328   /**
329    * Returns the trusted HTML ID of a single slick instance.
330    *
331    * @return string
332    *   The html ID.
333    *
334    * @todo: Consider Blazy::getHtmlId() instead.
335    */
336   public static function getHtmlId($string = 'slick', $id = '') {
337     if (!isset(static::$slickId)) {
338       static::$slickId = 0;
339     }
340
341     // Do not use dynamic Html::getUniqueId, otherwise broken asnavfors.
342     return empty($id) ? Html::getId($string . '-' . ++static::$slickId) : strip_tags($id);
343   }
344
345   /**
346    * Returns HTML or layout related settings to shut up notices.
347    *
348    * @return array
349    *   The default settings.
350    */
351   public static function htmlSettings() {
352     return [
353       'cache'             => 0,
354       'current_view_mode' => '',
355       'display'           => 'main',
356       'grid'              => 0,
357       'id'                => '',
358       'nav'               => FALSE,
359       'navpos'            => FALSE,
360       'media_switch'      => '',
361       'optionset'         => 'default',
362       'ratio'             => '',
363       'skin'              => '',
364       'unslick'           => FALSE,
365       'vanilla'           => FALSE,
366       'vertical'          => FALSE,
367       'vertical_tn'       => FALSE,
368       'view_name'         => '',
369     ];
370   }
371
372   /**
373    * Defines JS options required by theme_slick(), used with optimized option.
374    */
375   public static function jsSettings() {
376     return [
377       'asNavFor'        => '',
378       'downArrowTarget' => '',
379       'downArrowOffset' => '',
380       'lazyLoad'        => 'ondemand',
381       'prevArrow'       => '<button type="button" data-role="none" class="slick-prev" aria-label="Previous" tabindex="0" role="button">Previous</button>',
382       'nextArrow'       => '<button type="button" data-role="none" class="slick-next" aria-label="Next" tabindex="0" role="button">Next</button>',
383       'rows'            => 1,
384       'slidesPerRow'    => 1,
385       'slide'           => '',
386       'slidesToShow'    => 1,
387     ];
388   }
389
390 }