Skip to content

Commit 2a87145

Browse files
barshat7ci-build
authored andcommitted
support to embed forms in different sites
1 parent b8bd79e commit 2a87145

File tree

12 files changed

+284
-0
lines changed

12 files changed

+284
-0
lines changed

it/apps/src/main/content/META-INF/vault/filter.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<workspaceFilter version="1.0">
33
<filter root="/apps/forms-core-components-it"/>
44
<filter root="/apps/wknd"/>
5+
<filter root="/apps/forms-core-components-it/config"/>
56
<filter root="/apps/core/fd/af-clientlibs/core-forms-components-runtime-all/resources/i18n" mode="update"/>
67
</workspaceFilter>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
3+
jcr:primaryType="sling:Folder"/>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
3+
jcr:primaryType="nt:unstructured"
4+
contentTypes="[text/html]"
5+
enabled="{Boolean}true"
6+
generatorType="htmlparser"
7+
order="1"
8+
paths="[/content]"
9+
serializerType="htmlwriter"
10+
transformerTypes="[custom-linkrewriter]">
11+
<generator-htmlparser
12+
jcr:primaryType="nt:unstructured"
13+
includeTags="[LINK,SCRIPT]" />
14+
</jcr:root>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
3+
jcr:primaryType="cq:Component"
4+
jcr:title="Forms Embed Component"
5+
componentGroup=".Sites - Content"/>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## This component should be deployed in the Sites' instance where we need to embed the form
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
3+
jcr:primaryType="nt:unstructured"
4+
jcr:title="Properties"
5+
sling:resourceType="cq/gui/components/authoring/dialog">
6+
<content
7+
jcr:primaryType="nt:unstructured"
8+
sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns">
9+
<items jcr:primaryType="nt:unstructured">
10+
<column
11+
jcr:primaryType="nt:unstructured"
12+
sling:resourceType="granite/ui/components/coral/foundation/container">
13+
<items jcr:primaryType="nt:unstructured">
14+
<text
15+
jcr:primaryType="nt:unstructured"
16+
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
17+
fieldLabel="Forms URL - should be prefixed with abbviecloud"
18+
name="./formsUrl"/>
19+
</items>
20+
</column>
21+
</items>
22+
</content>
23+
</jcr:root>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<div>
2+
<!--TODO Replace the package name of the Model -->
3+
<div class="cmp-formsembed__item" data-sly-use.model="com.adobe.cq.forms.core.components.it.models.embed.FormsEmbed">
4+
<div class="cmp-formsembed-widget" data-forms-url=${model.formsUrl}>
5+
6+
<div class="afsection">
7+
<div data-sly-test="${wcmmode.edit}">
8+
Your Form Will Appear Here
9+
</div>
10+
</div>
11+
</div>
12+
</div>
13+
<div data-sly-test="${!wcmmode.edit}">
14+
<script>
15+
var updateElementContent = function (targetElement, newHtml) {
16+
targetElement.innerHTML = newHtml;
17+
Array.from(targetElement.querySelectorAll("script")).forEach(oldScriptElement => {
18+
const newScriptElement = document.createElement("script");
19+
Array.from(oldScriptElement.attributes).forEach(attribute => {
20+
newScriptElement.setAttribute(attribute.name, attribute.value);
21+
});
22+
const scriptText = document.createTextNode(oldScriptElement.innerHTML);
23+
newScriptElement.appendChild(scriptText);
24+
oldScriptElement.parentNode.replaceChild(newScriptElement, oldScriptElement);
25+
});
26+
}
27+
var formsUrl = document.querySelector('.cmp-formsembed-widget').getAttribute('data-forms-url');
28+
var options = {path:formsUrl, CSS_Selector:".afsection"};
29+
if (options.path) {
30+
fetch(options.path, {
31+
method: 'GET',
32+
// You can include additional headers or data here if needed
33+
})
34+
.then(response => {
35+
if (!response.ok) {
36+
throw new Error('Network response was not ok');
37+
}
38+
return response.text();
39+
})
40+
.then(data => {
41+
var element = document.querySelector(options.CSS_Selector);
42+
if (element) {
43+
updateElementContent(element, data);
44+
}
45+
})
46+
.catch(error => {
47+
// Error handling
48+
console.error('Error fetching data:', error);
49+
});
50+
} else {
51+
if (typeof console !== "undefined") {
52+
console.log("Path of Adaptive Form not specified to loadAdaptiveForm");
53+
}
54+
}
55+
</script>
56+
</div>
57+
</div>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.adobe.cq.forms.core.components.it.models.embed;
2+
3+
import com.adobe.cq.wcm.core.components.models.Component;
4+
import org.osgi.annotation.versioning.ConsumerType;
5+
6+
@ConsumerType
7+
public interface FormsEmbed extends Component {
8+
9+
String getFormsUrl();
10+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.adobe.cq.forms.core.components.it.models.embed;
2+
3+
import com.adobe.cq.export.json.ComponentExporter;
4+
import com.adobe.cq.export.json.ExporterConstants;
5+
import com.drew.lang.annotations.Nullable;
6+
import org.apache.sling.api.SlingHttpServletRequest;
7+
import org.apache.sling.models.annotations.Exporter;
8+
import org.apache.sling.models.annotations.Model;
9+
import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
10+
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
11+
12+
@Model(
13+
adaptables = SlingHttpServletRequest.class,
14+
adapters = {FormsEmbed.class, ComponentExporter.class},
15+
resourceType = FormsEmbedImpl.RESOURCE_TYPE
16+
)
17+
@Exporter(
18+
name = ExporterConstants.SLING_MODEL_EXPORTER_NAME,
19+
extensions = ExporterConstants.SLING_MODEL_EXTENSION
20+
)
21+
public class FormsEmbedImpl implements FormsEmbed {
22+
23+
// TODO replace app name
24+
public static final String RESOURCE_TYPE = "forms-core-component-it/components/formsembed";
25+
26+
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
27+
@Nullable
28+
protected String formsUrl;
29+
30+
31+
@Override
32+
public String getFormsUrl() {
33+
return formsUrl;
34+
}
35+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.adobe.cq.forms.core.components.it.service.rewriter;
2+
3+
import org.apache.sling.rewriter.DefaultTransformer;
4+
import org.apache.sling.rewriter.TransformerFactory;
5+
import org.osgi.service.component.annotations.Component;
6+
import org.osgi.service.component.annotations.Reference;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.xml.sax.Attributes;
10+
import org.xml.sax.SAXException;
11+
import org.xml.sax.helpers.AttributesImpl;
12+
13+
@Component(
14+
service = TransformerFactory.class,
15+
property = {
16+
"pipeline.type=" + CustomCloudRewriterTransformer.TYPE,
17+
}
18+
)
19+
public class CustomCloudRewriterTransformer implements TransformerFactory {
20+
21+
private static final Logger log = LoggerFactory.getLogger(CustomCloudRewriterTransformer.class);
22+
23+
public static final String TYPE = "custom-linkrewriter";
24+
25+
public static final String PATH_PREFIX = "custom";
26+
27+
28+
@Reference
29+
private CustomRunModeConfiguration customRunModeConfiguration;
30+
31+
@Override
32+
public TransformerImpl createTransformer() {
33+
String runmode = customRunModeConfiguration.getRunMode();
34+
log.info("current runmode = " + runmode);
35+
boolean isPublish = "publish".equals(runmode);
36+
return new TransformerImpl(isPublish);
37+
}
38+
39+
40+
protected static class TransformerImpl extends DefaultTransformer {
41+
42+
private final boolean isPublish;
43+
44+
public TransformerImpl(boolean isPublish) {
45+
super();
46+
this.isPublish = isPublish;
47+
}
48+
49+
@Override
50+
public void startElement(String uri, String localName, String qName, Attributes attributes)
51+
throws SAXException {
52+
AttributesImpl mutableAttributes =
53+
attributes instanceof AttributesImpl ? (AttributesImpl) attributes
54+
: null;
55+
if (isPublish) {
56+
if ("link".equals(localName)) {
57+
mutableAttributes = mutableAttributes != null ? mutableAttributes : new AttributesImpl(attributes);
58+
replaceLinkPaths(mutableAttributes);
59+
} else if ("script".equals(localName)) {
60+
mutableAttributes = mutableAttributes != null ? mutableAttributes : new AttributesImpl(attributes);
61+
replaceScriptPaths(mutableAttributes);
62+
}
63+
}
64+
super.startElement(uri, localName, qName,
65+
mutableAttributes != null ? mutableAttributes : attributes);
66+
}
67+
68+
private void replaceLinkPaths(AttributesImpl attributes) {
69+
if (attributes.getIndex("href") >= 0 && attributes.getIndex("rel") >= 0) {
70+
String rel = attributes.getValue("rel");
71+
if ("preload stylesheet".equals(rel) || "stylesheet".equals(rel)) {
72+
int index = attributes.getIndex("href");
73+
String value = attributes.getValue(index);
74+
setAttribute(attributes, index, addPathPrefix(value));
75+
}
76+
}
77+
}
78+
79+
private void replaceScriptPaths(AttributesImpl attributes) {
80+
if (attributes.getIndex("src") >= 0) {
81+
int index = attributes.getIndex("src");
82+
String value = attributes.getValue(index);
83+
setAttribute(attributes, index, addPathPrefix(value));
84+
}
85+
}
86+
87+
private String addPathPrefix(String url) {
88+
return "/" + PATH_PREFIX + url;
89+
}
90+
91+
private void setAttribute(AttributesImpl attributes, int index, String value) {
92+
String attrUri = attributes.getURI(index);
93+
String attrLocalName = attributes.getLocalName(index);
94+
String attrQName = attributes.getQName(index);
95+
String attrType = attributes.getType(index);
96+
attributes.setAttribute(index, attrUri, attrLocalName, attrQName, attrType, value);
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)