ResponsiveGrid.java
9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* 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);
}
}
}