You are here

class Slick in Slick Carousel 7.3

Same name and namespace in other branches
  1. 8.2 src/Entity/Slick.php \Drupal\slick\Entity\Slick
  2. 8 src/Entity/Slick.php \Drupal\slick\Entity\Slick

Defines the Slick configuration entity.

Hierarchy

Expanded class hierarchy of Slick

10 files declare their use of Slick
slick.api.php in ./slick.api.php
Hooks and API provided by the Slick module.
slick.deprecated.inc in ./slick.deprecated.inc
To be removed once Slick views, etc. have 3.x, or before Slick full release.
slick.install in ./slick.install
Installation actions for Slick.
slick.module in ./slick.module
Slick carousel integration, the last carousel you'll ever need.
slick.slick_optionset.inc in ./slick.slick_optionset.inc
Slick default optionset API include file.

... See full list

2 string references to 'Slick'
SlickCrudTest::getInfo in tests/SlickCrudTest.test
Provides info for the UI.
SlickLibrary::library in src/SlickLibrary.php
Implements hook_library_info_build().

File

src/Entity/Slick.php, line 8

Namespace

Drupal\slick\Entity
View source
class Slick extends SlickBase implements SlickInterface {

  /**
   * The optionset group for easy selections.
   *
   * @var string
   */
  public $collection = '';

  /**
   * The skin name for the optionset.
   *
   * @var string
   */
  public $skin = '';

  /**
   * The number of breakpoints for the optionset.
   *
   * @var int
   */
  public $breakpoints = 0;

  /**
   * The flag indicating to optimize the stored options by removing defaults.
   *
   * @var bool
   */
  public $optimized = 0;

  /**
   * {@inheritdoc}
   */
  public function getSkin() {
    return $this->skin;
  }

  /**
   * {@inheritdoc}
   */
  public function getBreakpoints() {
    return $this->breakpoints;
  }

  /**
   * {@inheritdoc}
   */
  public function getCollection() {
    return $this->collection;
  }

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

  /**
   * Returns the typecast values.
   *
   * @param array $settings
   *   An array of Optionset settings.
   */
  public static function typecast(array &$settings = []) {
    if (empty($settings)) {
      return;
    }
    $defaults = self::defaultSettings();
    foreach ($defaults as $name => $value) {
      if (isset($settings[$name])) {

        // Seems double is ignored, and causes a missing schema, unlike float.
        $type = gettype($defaults[$name]);
        $type = $type == 'double' ? 'float' : $type;

        // Change float to integer if value is no longer float.
        if ($name == 'edgeFriction') {
          $type = $settings[$name] == '1' ? 'integer' : 'float';
        }
        settype($settings[$name], $type);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    if (!isset(static::$defaultSettings)) {
      static::$defaultSettings = [
        'mobileFirst' => FALSE,
        'asNavFor' => '',
        'accessibility' => TRUE,
        'adaptiveHeight' => FALSE,
        'autoplay' => FALSE,
        'autoplaySpeed' => 3000,
        'pauseOnHover' => TRUE,
        'pauseOnDotsHover' => FALSE,
        'arrows' => TRUE,
        'prevArrow' => 'Previous',
        'nextArrow' => 'Next',
        'downArrow' => FALSE,
        'downArrowTarget' => '',
        'downArrowOffset' => 0,
        'centerMode' => FALSE,
        'centerPadding' => '50px',
        'dots' => FALSE,
        'dotsClass' => 'slick-dots',
        'appendDots' => '$(element)',
        'draggable' => TRUE,
        'fade' => FALSE,
        'focusOnSelect' => FALSE,
        'infinite' => TRUE,
        'initialSlide' => 0,
        'lazyLoad' => 'ondemand',
        'mouseWheel' => FALSE,
        'randomize' => FALSE,
        'respondTo' => 'window',
        'rows' => 1,
        'slidesPerRow' => 1,
        'slide' => '',
        'slidesToShow' => 1,
        'slidesToScroll' => 1,
        'speed' => 500,
        'swipe' => TRUE,
        'swipeToSlide' => FALSE,
        'edgeFriction' => 0.35,
        'touchMove' => TRUE,
        'touchThreshold' => 5,
        'useCSS' => TRUE,
        'cssEase' => 'ease',
        'cssEaseBezier' => '',
        'cssEaseOverride' => '',
        'useTransform' => TRUE,
        'easing' => 'linear',
        'variableWidth' => FALSE,
        'vertical' => FALSE,
        'verticalSwiping' => FALSE,
        'waitForAnimate' => TRUE,
      ];
    }
    return static::$defaultSettings;
  }

  /**
   * Returns default database field property values.
   *
   * @return mixed[]
   *   An array of property values, keyed by property name.
   */
  public static function defaultProperties() {
    return parent::defaultProperties() + [
      'breakpoints' => 0,
      'collection' => '',
      'optimized' => 0,
      'skin' => '',
    ];
  }

  /**
   * Returns the Slick responsive settings.
   *
   * @return array
   *   The responsive options.
   */
  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;
  }

  /**
   * Sets the Slick responsive settings.
   *
   * @return $this
   *   The class instance that this method is called on.
   */
  public function setResponsiveSettings($values, $delta = 0, $key = 'settings') {
    $this->options['responsives']['responsive'][$delta][$key] = $values;
    return $this;
  }

  /**
   * Strip out options containing default values so to have real clean JSON.
   *
   * @return array
   *   The cleaned out settings.
   */
  public function removeDefaultValues(array $js) {
    $config = [];
    $defaults = self::defaultSettings();

    // Remove wasted dependent options if disabled, empty or not.
    if (!$this->optimized) {
      $this
        ->removeWastedDependentOptions($js);
    }
    $config = array_diff_assoc($js, $defaults);

    // Remove empty lazyLoad, or left to default ondemand, to avoid JS error.
    if (empty($config['lazyLoad'])) {
      unset($config['lazyLoad']);
    }

    // Do not pass arrows HTML to JSON object as some are enforced.
    $excludes = [
      'downArrow',
      'downArrowTarget',
      'downArrowOffset',
      'prevArrow',
      'nextArrow',
    ];
    foreach ($excludes as $key) {
      unset($config[$key]);
    }

    // Clean up responsive options if similar to defaults.
    if ($responsives = $this
      ->getResponsiveOptions()) {
      $cleaned = [];
      foreach ($responsives as $key => $responsive) {
        $cleaned[$key]['breakpoint'] = $responsives[$key]['breakpoint'];

        // Destroy responsive slick if so configured.
        if (!empty($responsives[$key]['unslick'])) {
          $cleaned[$key]['settings'] = 'unslick';
          unset($responsives[$key]['unslick']);
        }
        else {

          // Remove wasted dependent options if disabled, empty or not.
          if (!$this->optimized) {
            $this
              ->removeWastedDependentOptions($responsives[$key]['settings']);
          }
          $cleaned[$key]['settings'] = array_diff_assoc($responsives[$key]['settings'], $defaults);
        }
      }
      $config['responsive'] = $cleaned;
    }
    return $config;
  }

  /**
   * Removes wasted dependent options, even if not empty.
   */
  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']);
  }

  /**
   * Defines the dependent options.
   *
   * @return array
   *   The dependent options.
   */
  public static function getDependentOptions() {
    $down_arrow = [
      'downArrowTarget',
      'downArrowOffset',
    ];
    return [
      'arrows' => [
        'prevArrow',
        'nextArrow',
        'downArrow',
      ] + $down_arrow,
      'downArrow' => $down_arrow,
      'autoplay' => [
        'pauseOnHover',
        'pauseOnDotsHover',
        'autoplaySpeed',
      ],
      'centerMode' => [
        'centerPadding',
      ],
      'dots' => [
        'dotsClass',
        'appendDots',
      ],
      'swipe' => [
        'swipeToSlide',
      ],
      'useCSS' => [
        'cssEase',
        'cssEaseBezier',
        'cssEaseOverride',
      ],
      'vertical' => [
        'verticalSwiping',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Slick::$breakpoints public property The number of breakpoints for the optionset.
Slick::$collection public property The optionset group for easy selections.
Slick::$optimized public property The flag indicating to optimize the stored options by removing defaults.
Slick::$skin public property The skin name for the optionset.
Slick::defaultProperties public static function Returns default database field property values. Overrides SlickBase::defaultProperties
Slick::defaultSettings public static function Returns available slick default options under group 'settings'. Overrides SlickBase::defaultSettings
Slick::getBreakpoints public function Returns the number of breakpoints. Overrides SlickInterface::getBreakpoints
Slick::getCollection public function Returns the group this optioset instance belongs to for easy selections. Overrides SlickInterface::getCollection
Slick::getDependentOptions public static function Defines the dependent options.
Slick::getResponsiveOptions public function Returns the Slick responsive settings.
Slick::getSkin public function Returns the Slick skin. Overrides SlickInterface::getSkin
Slick::optimized public function Returns whether to optimize the stored options, or not. Overrides SlickInterface::optimized
Slick::removeDefaultValues public function Strip out options containing default values so to have real clean JSON.
Slick::removeWastedDependentOptions public function Removes wasted dependent options, even if not empty.
Slick::setResponsiveSettings public function Sets the Slick responsive settings.
Slick::typecast public static function Returns the typecast values. Overrides SlickBase::typecast
SlickBase::$defaultSettings protected static property The plugin default settings. 1
SlickBase::$label public property The human-readable name for the optionset.
SlickBase::$name public property The legacy CTools ID for the configurable optionset.
SlickBase::$options public property The plugin instance options.
SlickBase::create public static function Returns a new optionset object without saving it to the database. Overrides SlickBaseInterface::create
SlickBase::delete public function Deletes the optionset from database.
SlickBase::exists public static function Checks whether an optionset with the given name already exists. Overrides SlickBaseInterface::exists
SlickBase::getOptions public function Returns the Slick options by group, or property. Overrides SlickBaseInterface::getOptions
SlickBase::getSetting public function Returns the value of a slick setting. Overrides SlickBaseInterface::getSetting
SlickBase::getSettings public function Returns the array of slick settings. Overrides SlickBaseInterface::getSettings
SlickBase::id public function Overrides Drupal\Core\Entity\Entity::id().
SlickBase::label public function The slick label.
SlickBase::load public static function Returns the given optionset object identified by $id. Overrides SlickBaseInterface::load
SlickBase::loadMultiple public static function Fetches all optionsets from the storage. Overrides SlickBaseInterface::loadMultiple
SlickBase::loadWithFallback public static function Load the optionset with a fallback.
SlickBase::save public function Saves the optionset to database.
SlickBase::setSetting public function Sets the value of a slick setting. Overrides SlickBaseInterface::setSetting
SlickBase::setSettings public function Sets the array of slick settings. Overrides SlickBaseInterface::setSettings
SlickBase::TABLE constant Defines slick table name.
SlickBase::toArray public function Returns an array of all property values.