ResponsiveGrid.java 9.93 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.sightly.WCMUsePojo
 *  com.day.cq.wcm.api.TemplatedResource
 *  com.day.cq.wcm.api.components.Component
 *  com.day.cq.wcm.api.designer.Style
 *  com.day.cq.wcm.commons.WCMUtils
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 */
package com.day.cq.wcm.foundation;

import com.adobe.cq.sightly.WCMUsePojo;
import com.day.cq.wcm.api.TemplatedResource;
import com.day.cq.wcm.api.components.Component;
import com.day.cq.wcm.api.designer.Style;
import com.day.cq.wcm.commons.WCMUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;

public class ResponsiveGrid
extends WCMUsePojo {
    private final String DEFAULT_CSS_PREFIX = "aem-Grid";
    private static final int DEFAULT_COLUMNS = 12;
    private static final String BREAKPOINT_VARIANT_NAME_DEFAULT = "default";
    private String cssClassPrefix;
    private int columns;
    private String cssClass;
    private List<Column> paragraphs = new ArrayList<Column>();

    public void activate() throws Exception {
        Style currentStyle = this.getCurrentStyle();
        ValueMap properties = this.getProperties();
        HashMap<String, Integer> breakpoints = new HashMap<String, Integer>();
        HashSet<String> missingParagraphBreakpointNames = new HashSet<String>();
        this.cssClassPrefix = (String)currentStyle.get("cssPrefix", (Object)"aem-Grid");
        this.cssClass = "aem-Grid";
        Resource responsiveParentCfg = this.getResource().getParent().getChild("cq:responsive");
        Resource responsiveCfg = this.getResource().getChild("cq:responsive");
        boolean hasResponsiveCfgWidth = false;
        if (responsiveCfg != null) {
            Iterator resCfgIt = responsiveCfg.listChildren();
            while (resCfgIt.hasNext()) {
                Resource parentBreakpoint;
                Resource resCfg = (Resource)resCfgIt.next();
                String breakpointName = resCfg.getName();
                ValueMap cfg = (ValueMap)resCfg.adaptTo(ValueMap.class);
                int width = (Integer)cfg.get("width", (Object)0);
                if (responsiveParentCfg != null && (parentBreakpoint = responsiveParentCfg.getChild(breakpointName)) != null) {
                    ValueMap parentCfg = (ValueMap)parentBreakpoint.adaptTo(ValueMap.class);
                    int parentWidth = (Integer)parentCfg.get("width", (Object)width);
                    width = width > parentWidth ? parentWidth : width;
                }
                breakpoints.put(breakpointName, width);
                if (width <= 0) continue;
                hasResponsiveCfgWidth = true;
                this.cssClass = this.cssClass + " aem-Grid--" + breakpointName + "--" + width;
            }
        }
        if (!hasResponsiveCfgWidth) {
            int columns = (Integer)currentStyle.get("columns", (Object)12);
            this.cssClass = this.cssClass + " aem-Grid--" + columns;
        }
        if (!breakpoints.containsKey("default")) {
            int columns = (Integer)currentStyle.get("columns", (Object)12);
            breakpoints.put("default", columns);
            this.cssClass = this.cssClass + " aem-Grid--default--12";
        }
        Iterator paragraphsIt = this.getEffectiveResource().listChildren();
        while (paragraphsIt.hasNext()) {
            Resource child = (Resource)paragraphsIt.next();
            if ("cq:responsive".equals(child.getName())) continue;
            Column column = child instanceof TemplatedResource ? new Column(this, ((TemplatedResource)child).getResource(), breakpoints) : new Column(this, child, breakpoints);
            this.paragraphs.add(column);
            missingParagraphBreakpointNames.addAll(column.getMissingBreakpointNames());
        }
        if (!missingParagraphBreakpointNames.isEmpty()) {
            for (String missingParagraphBreakpointName : missingParagraphBreakpointNames) {
                Integer columnCount = (Integer)breakpoints.get("default");
                if (columnCount == null) {
                    columnCount = (Integer)currentStyle.get("columns", (Object)12);
                }
                this.cssClass = this.cssClass + " aem-Grid--" + missingParagraphBreakpointName + "--" + columnCount;
            }
        }
        this.cssClass = this.cssClass + " " + (String)properties.get("cq:cssClass", (Object)"");
    }

    public <T extends Resource> T getEffectiveResource() {
        Resource templatedResource = (Resource)this.getRequest().adaptTo(TemplatedResource.class);
        if (templatedResource == null) {
            return (T)this.getResource();
        }
        return (T)templatedResource;
    }

    public String getCssClass() {
        return this.cssClass;
    }

    public List<Column> getParagraphs() {
        return this.paragraphs;
    }

    public class Column {
        private Resource resource;
        private Map<String, Integer> gridBreakpoints;
        private String cssClass;
        private Map<String, Integer> breakpoints;
        private Set<String> columnOnlyBreakpointNames;
        private String columnCssClassPrefix;
        final /* synthetic */ ResponsiveGrid this$0;

        public Column(ResponsiveGrid responsiveGrid, Resource res) {
            this.this$0 = responsiveGrid;
            this.resource = res;
            this.columnCssClassPrefix = responsiveGrid.cssClassPrefix + "Column";
            this.createCssClass();
        }

        public Column(ResponsiveGrid responsiveGrid, Resource res, Map<String, Integer> gridBreakpoints) {
            this.this$0 = responsiveGrid;
            this.resource = res;
            this.gridBreakpoints = gridBreakpoints;
            this.columnCssClassPrefix = responsiveGrid.cssClassPrefix + "Column";
            this.createCssClass();
        }

        public Resource getResource() {
            return this.resource;
        }

        public String getPath() {
            return this.resource.getPath();
        }

        public Set<String> getMissingBreakpointNames() {
            return this.columnOnlyBreakpointNames;
        }

        public Integer getColumnCount(String breakpointName) {
            return this.breakpoints.get(breakpointName);
        }

        private void createCssClass() {
            String cssClass = this.columnCssClassPrefix;
            this.columnOnlyBreakpointNames = new HashSet<String>();
            Component component = WCMUtils.getComponent((Resource)this.resource);
            ValueMap componentProperties = null;
            if (component != null) {
                componentProperties = component.getProperties();
            }
            Resource responsiveCfg = this.resource.getChild("cq:responsive");
            this.breakpoints = new HashMap<String, Integer>();
            if (responsiveCfg != null) {
                Iterator resCfgIt = responsiveCfg.listChildren();
                while (resCfgIt.hasNext()) {
                    Resource resCfg = (Resource)resCfgIt.next();
                    String breakpointName = resCfg.getName();
                    ValueMap cfg = (ValueMap)resCfg.adaptTo(ValueMap.class);
                    int width = (Integer)cfg.get("width", (Object)0);
                    String behavior = (String)cfg.get("behavior", String.class);
                    this.breakpoints.put(breakpointName, width);
                    if (width > 0) {
                        cssClass = cssClass + " " + this.columnCssClassPrefix + "--" + breakpointName + "--" + width;
                    }
                    if (behavior == null) continue;
                    cssClass = cssClass + " " + this.columnCssClassPrefix + "--" + breakpointName + "--" + behavior;
                }
            }
            if (this.gridBreakpoints != null && !this.gridBreakpoints.isEmpty()) {
                ArrayList<String> missingGridBreakpointNames = new ArrayList<String>(this.gridBreakpoints.keySet());
                missingGridBreakpointNames.removeAll(this.breakpoints.keySet());
                for (String missingGridBreakpointName : missingGridBreakpointNames) {
                    Integer width = this.breakpoints.get("default");
                    Integer gridWidth = this.gridBreakpoints.get(missingGridBreakpointName);
                    if (width == null || gridWidth != null && width > gridWidth) {
                        width = gridWidth;
                    }
                    if (width == null || width > 12) {
                        width = 12;
                    }
                    cssClass = cssClass + " " + this.columnCssClassPrefix + "--" + missingGridBreakpointName + "--" + width;
                }
                ArrayList<String> missingBreakpointNames = new ArrayList<String>(this.breakpoints.keySet());
                missingBreakpointNames.removeAll(this.gridBreakpoints.keySet());
                for (String missingBreakPointName : missingBreakpointNames) {
                    this.columnOnlyBreakpointNames.add(missingBreakPointName);
                }
            } else {
                this.columnOnlyBreakpointNames.addAll(this.breakpoints.keySet());
            }
            if (!this.breakpoints.keySet().contains("default")) {
                cssClass = cssClass + " " + this.columnCssClassPrefix + "--" + "default" + "--" + 12;
            }
            if (componentProperties != null) {
                cssClass = cssClass + " " + (String)componentProperties.get("cq:cssClass", (Object)"");
            }
            this.cssClass = cssClass;
        }

        public String getCssClass() {
            return this.cssClass;
        }

        public ValueMap getProperties() {
            return (ValueMap)this.resource.adaptTo(ValueMap.class);
        }
    }

}