All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.aemds.guide.model.ResponsiveConfiguration Maven / Gradle / Ivy

/*
 *
 * ADOBE CONFIDENTIAL
 * ___________________
 *
 * Copyright 2018 Adobe Systems Incorporated
 * All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */

package com.adobe.aemds.guide.model;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * @pad.exclude Exclude from Published API.
 */
@Model(adaptables =  Resource.class)
public class ResponsiveConfiguration {

  @Inject @Optional
  @Named("./default")
  private BreakPointResponsiveConfiguration defaultConfiguration;

  @Inject @Optional
  @Named("./tablet")
  private BreakPointResponsiveConfiguration tabletConfiguration;

  @Inject @Optional
  @Named("./phone")
  private BreakPointResponsiveConfiguration phoneConfiguration;

  @Inject @Optional
  @Named("./smallScreen")
  private BreakPointResponsiveConfiguration smallScreenConfiguration;

  private static String CSS_CLASS_SEPARATOR = " ";
  private static String CSS_FD_PREFIX = "fd";
  private static String CSS_CLASS_PREFIX = CSS_FD_PREFIX + "-col";
  private static String OFFSET_PREFIX = "offset-";
  private static String SCREEN_SIZE_LARGE = "-lg-";
  private static String SCREEN_SIZE_MEDIUM = "-md-";
  private static String SCREEN_SIZE_SMALL = "-sm-";
  private static String SCREEN_SIZE_EXTRA_SMALL = "-xs-";
  private static String NEWLINE_BEHAVIOR = "newline";
  private static String DEFAULT_OFFSET = "0";
  private static Integer DEFAULT_WIDTH = 12;
  private static String NEWLINE_CLASS = "newline";

  private static Logger logger = LoggerFactory.getLogger(ResponsiveConfiguration.class);

  public BreakPointResponsiveConfiguration getDefaultConfiguration() {
    return defaultConfiguration;
  }

  public BreakPointResponsiveConfiguration getTabletConfiguration() {
    return tabletConfiguration;
  }

  public BreakPointResponsiveConfiguration getPhoneConfiguration() {
    return phoneConfiguration;
  }

  public BreakPointResponsiveConfiguration getSmallScreenConfiguration() {
    return smallScreenConfiguration;
  }

  public String getResponsiveClasses() {
    StringBuilder responsiveClassBuilder = new StringBuilder();
    String defaultClass = getCssClass(defaultConfiguration, null, SCREEN_SIZE_LARGE);
    String mediumClass = getCssClass(tabletConfiguration, defaultConfiguration, SCREEN_SIZE_MEDIUM);
    String smallClass = getCssClass(phoneConfiguration, null, SCREEN_SIZE_SMALL);
    String extraSmallClass = getCssClass(smallScreenConfiguration, phoneConfiguration, SCREEN_SIZE_EXTRA_SMALL);
    responsiveClassBuilder.append(defaultClass).append(CSS_CLASS_SEPARATOR)
            .append(mediumClass).append(CSS_CLASS_SEPARATOR)
            .append(smallClass).append(CSS_CLASS_SEPARATOR)
            .append(extraSmallClass);
    return  responsiveClassBuilder.toString();
  }

  private String getCssClass(BreakPointResponsiveConfiguration input,
                          BreakPointResponsiveConfiguration fallback,
                          String screenSize) {
    String fallbackWidth = null;
    String fallbackOffset = null;
    String fallbackBehavior = null;
    if (fallback != null) {
      fallbackWidth = fallback.getWidth();
      fallbackOffset = fallback.getOffset();
      fallbackBehavior = fallback.getBehavior();
    }

    String inputWidth = null;
    String inputOffset = null;
    String inputBehavior = null;
    if (input != null) {
      inputWidth = input.getWidth();
      inputOffset = input.getOffset();
      inputBehavior = input.getBehavior();
    }

    String width = getOrElse(inputWidth, fallbackWidth);
    String offset = getOrElse(inputOffset, fallbackOffset);
    String behavior = getOrElse(inputBehavior, fallbackBehavior);
    return getCssClass(screenSize, width, offset, behavior);
  }

  private String getCssClass(String screenSize, String width, String offset, String behavior) {
    StringBuilder cssClassBuilder = new StringBuilder();
    String cssScreenPrefix = CSS_CLASS_PREFIX + screenSize;

    if (width == null) {
      Integer widthValue = DEFAULT_WIDTH;
      if (offset != null) {
        try {
          Integer offsetValue = Integer.parseInt(offset);
          widthValue = widthValue - offsetValue;
        } catch (NumberFormatException nfe) {
          //Ignore the offset
          logger.error("Ignoring offset: " + offset + ", as it can't be parsed as number : ", nfe);
        }
      }
      width = widthValue.toString();
    }
    cssClassBuilder.append(cssScreenPrefix).append(width);

    if (offset == null) {
      offset = DEFAULT_OFFSET;
    }
    cssClassBuilder.append(CSS_CLASS_SEPARATOR)
            .append(cssScreenPrefix).append(OFFSET_PREFIX).append(offset);

    if (NEWLINE_BEHAVIOR.equals(behavior)) {
      cssClassBuilder.append(CSS_CLASS_SEPARATOR)
              .append(CSS_FD_PREFIX).append(screenSize).append(NEWLINE_CLASS);
    }
    return  cssClassBuilder.toString();
  }

  private String getOrElse(String inputValue, String fallbackValue) {
    String returnValue = null;
    if (StringUtils.isNotEmpty(inputValue)) {
      returnValue = inputValue;
    } else if (StringUtils.isNotEmpty(fallbackValue)) {
      returnValue = fallbackValue;
    }
    return returnValue;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy